📜  将Array的每个元素替换为其相应的等级

📅  最后修改于: 2021-04-27 23:55:14             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是将Array中的每个元素替换为它们在array中的排名。

例子:

天真的方法:天真的方法是找到每个元素的等级为1 +当前元素数组中较小元素数量

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:要优化上述幼稚方法,请找到元素的等级,然后将等级分配给这些元素。步骤如下:

  1. 要计算元素的等级,请首先复制给定的arr [],然后按升序对复制的数组进行排序。
  2. 然后遍历复制的数组,并通过使用rank变量将其等级放入HashMap中
  3. 如果该元素已经存在于HashMap中,则不要更新等级,否则更新该元素在HashMap中的等级以及递增rank变量。
  4. 遍历给定数组arr []使用存储在HashMap中的等级分配每个元素的等级。

下面是上述方法的实现:

C++
// C++ program for the above approach 
#include 
using namespace std;
  
// Function to assign rank to 
// array eleements 
void changeArr(int input[], int N) 
{ 
      
    // Copy input array into newArray 
    int newArray[N];
    copy(input, input + N, newArray);
  
    // Sort newArray[] in ascending order 
    sort(newArray, newArray + N); 
    int i; 
      
    // Map to store the rank of 
    // the array element 
    map ranks;
  
    int rank = 1; 
  
    for(int index = 0; index < N; index++)
    { 
  
        int element = newArray[index]; 
  
        // Update rank of element 
        if (ranks[element] == 0) 
        { 
            ranks[element] = rank; 
            rank++; 
        } 
    } 
  
    // Assign ranks to elements 
    for(int index = 0; index < N; index++) 
    { 
        int element = input[index]; 
        input[index] = ranks[input[index]]; 
    } 
} 
  
// Driver code    
int main()
{
      
    // Given array arr[] 
    int arr[] = { 100, 2, 70, 2 }; 
  
    int N = sizeof(arr) / sizeof(arr[0]);
      
    // Function call 
    changeArr(arr, N); 
  
    // Print the array elements 
    cout << "[";
    for(int i = 0; i < N - 1; i++)
    {
        cout << arr[i] << ", ";
    }
    cout << arr[N - 1] << "]";
    return 0;
}
  
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach 
import java.util.*; 
  
class GFG { 
  
    // Function to assign rank to 
    // array eleements 
    static void changeArr(int[] input) 
    { 
        // Copy input array into newArray 
        int newArray[] 
            = Arrays 
                .copyOfRange(input, 
                            0, 
                            input.length); 
  
        // Sort newArray[] in ascending order 
        Arrays.sort(newArray); 
        int i; 
          
        // Map to store the rank of 
        // the array element 
        Map ranks 
            = new HashMap<>(); 
  
        int rank = 1; 
  
        for (int index = 0; 
            index < newArray.length; 
            index++) { 
  
            int element = newArray[index]; 
  
            // Update rank of element 
            if (ranks.get(element) == null) { 
  
                ranks.put(element, rank); 
                rank++; 
            } 
        } 
  
        // Assign ranks to elements 
        for (int index = 0; 
            index < input.length; 
            index++) { 
  
            int element = input[index]; 
            input[index] 
                = ranks.get(input[index]); 
          
        } 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
        // Given array arr[] 
        int[] arr = { 100, 2, 70, 2 }; 
  
        // Function Call 
        changeArr(arr); 
  
        // Print the array elements 
        System 
            .out 
            .println(Arrays 
                        .toString(arr)); 
    } 
}


Python3
# Python3 program for the above approach 
  
# Function to assign rank to 
# array eleements 
def changeArr(input1): 
  
    # Copy input array into newArray 
    newArray = input1.copy() 
      
    # Sort newArray[] in ascending order 
    newArray.sort() 
      
    # Dictionary to store the rank of 
    # the array element 
    ranks = {} 
      
    rank = 1
      
    for index in range(len(newArray)): 
        element = newArray[index]; 
      
        # Update rank of element 
        if element not in ranks: 
            ranks[element] = rank 
            rank += 1
          
    # Assign ranks to elements 
    for index in range(len(input1)): 
        element = input1[index] 
        input1[index] = ranks[input1[index]] 
  
# Driver Code 
if __name__ == "__main__": 
      
    # Given array arr[] 
    arr = [ 100, 2, 70, 2 ] 
      
    # Function call 
    changeArr(arr) 
      
    # Print the array elements 
    print(arr) 
  
# This code is contributed by chitranayal


C#
// C# program for the above approach 
using System; 
using System.Collections.Generic; 
using System.Text; 
  
class GFG{ 
  
// Function to assign rank to 
// array eleements 
static void changeArr(int[] input) 
{ 
      
    // Copy input array into newArray 
    int []newArray = new int[input.Length]; 
    Array.Copy(input, newArray, input.Length); 
  
    // Sort newArray[] in ascending order 
    Array.Sort(newArray); 
      
    // To store the rank of 
    // the array element 
    Dictionary ranks= new Dictionary(); 
  
    int rank = 1; 
  
    for(int index = 0; 
            index < newArray.Length; 
            index++) 
    { 
        int element = newArray[index]; 
  
        // Update rank of element 
        if (!ranks.ContainsKey(element)) 
        { 
            ranks[element] = rank; 
            rank++; 
        } 
    } 
  
    // Assign ranks to elements 
    for(int index = 0; 
            index < input.Length; 
            index++) 
    { 
        input[index] = ranks[input[index]]; 
    } 
} 
  
// Driver Code 
public static void Main(string[] args) 
{ 
      
    // Given array arr[] 
    int[] arr = { 100, 2, 70, 2 }; 
  
    // Function call 
    changeArr(arr); 
  
    // Print the array elements 
    Console.WriteLine("[{0}]", 
        string.Join(", ", arr)); 
} 
} 
  
// This code is contributed by rutvik_56


[3, 1, 2, 1]

时间复杂度: O(N * log N)
辅助空间: O(N)