📜  用对应的等级替换 Array 的每个元素

📅  最后修改于: 2021-10-26 06:04:44             🧑  作者: Mango

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

例子:

朴素的方法:朴素的方法是找到每个元素的秩为1 +当前元素的数组中较小元素计数
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:为了优化上述朴素的方法,找到元素的等级,然后将等级分配给元素。以下是步骤:

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

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to assign rank to
// array elements
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 elements
    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 elements
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 elements
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


Javascript


[3, 1, 2, 1]

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