首页 > 其他分享 >LeetCode 1331. Rank Transform of an Array

LeetCode 1331. Rank Transform of an Array

时间:2024-04-21 22:56:57浏览次数:24  
标签:arr int 1331 rank Rank hm 100 Array LeetCode

原题链接在这里:https://leetcode.com/problems/rank-transform-of-an-array/description/

题目:

Given an array of integers arr, replace each element with its rank.

The rank represents how large the element is. The rank has the following rules:

  • Rank is an integer starting from 1.
  • The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
  • Rank should be as small as possible.

Example 1:

Input: arr = [40,10,20,30]
Output: [4,1,2,3]
Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.

Example 2:

Input: arr = [100,100,100]
Output: [1,1,1]
Explanation: Same elements share the same rank.

Example 3:

Input: arr = [37,12,28,9,100,56,80,5,12]
Output: [5,3,4,2,8,6,7,1,3]

Constraints:

  • 0 <= arr.length <= 105
  • -109 <= arr[i] <= 109

题解:

To get the rank, we need to sort the array and put its value to rank into a map. 

But we need to maintain the origianl index, thus we need to make a copy first.

Time Complexity: O(nlogn). n = arr.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] arrayRankTransform(int[] arr) {
 3         int[] arrCopy = Arrays.copyOf(arr, arr.length);
 4         Arrays.sort(arrCopy);
 5         Map<Integer, Integer> hm = new HashMap<>();
 6         for(int num : arrCopy){
 7             hm.putIfAbsent(num, hm.size() + 1);
 8         }
 9 
10         for(int i = 0; i < arr.length; i++){
11             arr[i] = hm.get(arr[i]);
12         }
13 
14         return arr;
15     }
16 }

 

标签:arr,int,1331,rank,Rank,hm,100,Array,LeetCode
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18149681

相关文章

  • Arrays类
    importjava.util.Arrays;publicclassDemo05{publicstaticvoidmain(String[]args){int[]a={111,2,5,6,999,7777,2,33,};//打印数组元素--Arrays.toString//System.out.println(Arrays.toString(a));//printArray(a);Arrays.sort(a);//数组进行排......
  • [leetcode 周赛] 100276. 最短路径中的边
    solution使用dijkstra算法求出顶点0到每个顶点的最小距离dp[i]然后再从n-1开始遍历,如果dp[to]+w==dp[from],这条边符合条件importjava.util.*;classSolution{publicstaticvoidmain(String[]args){Solutionsolution=newSolution();......
  • LeetCode 2326. Spiral Matrix IV
    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iv/description/题目:Youaregiventwointegers m and n,whichrepresentthedimensionsofamatrix.Youarealsogiventhe head ofalinkedlistofintegers.Generatean mxn matrixthatconta......
  • LeetCode 1424. Diagonal Traverse II
    原题链接在这里:https://leetcode.com/problems/diagonal-traverse-ii/description/题目:Givena2Dintegerarray nums,return allelementsof nums indiagonalorderasshowninthebelowimages.Example1:Input:nums=[[1,2,3],[4,5,6],[7,8,9]]Output:[1,4,......
  • typescript安装问题=> for (let i = startIndex ?? 0; i < array.length; i++) {
    for(leti=startIndex??0;i<array.length;i++){^SyntaxError:Unexpectedtoken?atObject.exports.runInThisContext(vm.js:76:16)atModule._compile(module.js:542:28)atObject.Module._extensions..js(mo......
  • LeetCode三则
    63.不同路径II一个机器人位于一个mxn网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?网格中的障碍物和空......
  • leetcode回文数
    给你一个整数x,如果x是一个回文整数,返回true;否则,返回false。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121是回文,而123不是。示例1:输入:x=121输出:true示例2:输入:x=-121输出:false解释:从左向右读,为-121。从右向左读,为121-。因此......
  • CF1713F Lost Array 题解
    题目链接点击打开链接题目解法很牛的题!!!先考虑\((0,i)\)对\((j,n)\)的贡献,因为是异或,所以只要考虑奇偶性问题可以抽象成一条路径对应\(a_i\)的贡献,所以是否有\(a_i\)的贡献看\(\binom{n-i+j-1}{j-1}\)的奇偶性根据\(kummer\)定理,这个组合数是奇数当且仅当\(n-i+......
  • LeetCode三则
    三道动态规划62.不同路径一个机器人位于一个mxn网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。问总共有多少条不同的路径?输入:m=3,n=7输出:28输入:m=3,n=2输出:3解释:......
  • LeetCode 面试经典150题---008
    ####151.反转字符串中的单词给你一个字符串s,请你反转字符串中单词的顺序。单词是由非空格字符组成的字符串。s中使用至少一个空格将字符串中的单词分隔开。返回单词顺序颠倒且单词之间用单个空格连接的结果字符串。注意:输入字符串s中可能会存在前导空格、尾随空格......