当前位置:网站首页>Leetcode 1331. array sequence number conversion
Leetcode 1331. array sequence number conversion
2022-07-28 14:15:00 【Tisfy】
【LetMeFly】1331. Array number conversion
Force button topic link :https://leetcode.cn/problems/rank-transform-of-an-array/
Give you an array of integers arr , Please replace each element in the array with their ordinal number after sorting .
The serial number represents how big an element is . The rules for serial numbers are as follows :
- The serial number from 1 Numbered starting .
- The bigger an element is , So the bigger the serial number . If two elements are equal , So they have the same serial number .
- The serial number of each number should be as small as possible .
Example 1:
Input :arr = [40,10,20,30] Output :[4,1,2,3] explain :40 It's the biggest element . 10 It's the smallest element . 20 It's the second smallest number . 30 It's the third smallest number .
Example 2:
Input :arr = [100,100,100] Output :[1,1,1] explain : All elements have the same sequence number .
Example 3:
Input :arr = [37,12,28,9,100,56,80,5,12] Output :[5,3,4,2,8,6,7,1,3]
Tips :
0 <= arr.length <= 105-109 <= arr[i] <= 109
Method 1 :sort + Hashtable
First, copy a copy of the original array into the temporary array , And sort the temporary array
After ordering , Traverse the temporary array , Record your ranking ( Use a variable last To store the last value , If this value is different from the previous one, it will rank +1) To hash table
Traverse the original array , Modify the value “ Map from the hash table to the value after ranking ”
- Time complexity O ( n log n ) O(n\log n) O(nlogn), among n n n Is the number of elements in the array . Sorting time n log n n\log n nlogn
- Spatial complexity O ( N log N ) O(N\log N) O(NlogN). Hashtable 、 Sorting and temporary arrays consume space O ( n ) O(n) O(n)
AC Code
C++
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> toSort = arr;
sort(toSort.begin(), toSort.end());
unordered_map<int, int> ma;
int th = 0;
int last = -1e9 - 1;
for (int i = 0; i < toSort.size(); i++) {
if (toSort[i] == last)
continue;
ma[toSort[i]] = ++th;
last = toSort[i];
}
for (int& t : arr) {
t = ma[t];
}
return arr;
}
};
Python
Grammar sugar is so simple
Python Code Copy From Try to answer the official questions
class Solution:
def arrayRankTransform(self, arr: List[int]) -> List[int]:
ranks = {
v: i for i, v in enumerate(sorted(set(arr)), 1)}
return [ranks[v] for v in arr]
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/126030218
边栏推荐
- [try to hack] hfish honeypot deployment
- 【LVGL事件(Events)】事件代码
- 【Utils】CookieUtil
- 对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
- 线程阻塞的三种情况。
- Do you really know esmodule
- 《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
- 作为一个程序员,如何高效的管理时间?
- Record a fake login of cookie
- Security assurance is based on software life cycle -istio authorization mechanism
猜你喜欢

Entering the world of audio and video -- flv video packaging format

Qt5开发从入门到精通——第一篇概述

阿里、京东、抖音:把云推向产业心脏

Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)

Security assurance is based on software life cycle -istio authentication mechanism

安全保障基于软件全生命周期-NetworkPolicy应用

Qt5 development from introduction to mastery -- the first overview

作为一个程序员,如何高效的管理时间?

Clickhouse architecture and design

安全保障基于软件全生命周期-PSP应用
随机推荐
【LVGL事件(Events)】事件代码
Several efficient APIs commonly used in inventory operation URL
Niuke multi school link with level editor i- (linear DP)
Vite configuring path aliases in the project
Chapter 6 support vector machine
【LVGL事件(Events)】事件在不同组件上的应用(一)
Jmeter安装教程及登录增加token
QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
Postgresql14安装及主从配置
对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
Understand the principle behind the virtual list, and easily realize the virtual list
QML picture preview
盘点操作URL中常用的几个高效API
83. (cesium home) how the cesium example works
安全保障基于软件全生命周期-Istio的认证机制
记一次COOKIE的伪造登录
Clickhouse distributed cluster construction
[utils] fastdfs tool class
strcmp、strstr、memcpy、memmove的实现