当前位置:网站首页>[leetcode] 1331. Array sequence number conversion
[leetcode] 1331. Array sequence number conversion
2022-07-28 14:21:00 【pass night】
subject
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
Ideas
- Sort the array , Take the sorted subscript as the position of the array
- When encountering repeated numbers, the number of bits will not increase
Code
class Solution:
def arrayRankTransform(self, arr: List[int]) -> List[int]:
indexMap = {
}
ret = []
index = 1
for n in sorted(arr):
if n not in indexMap:
indexMap[n] = index
index += 1
for n in arr:
ret.append(indexMap[n])
return ret
Complexity
- Time complexity : O ( n log n ) O(n\log n) O(nlogn)
- Spatial complexity : O ( n ) O(n) O(n)
边栏推荐
- HCIP第十一天
- 【Utils】JsonUtil
- Security assurance is based on software life cycle -psp application
- RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
- 走进音视频的世界——FLV视频封装格式
- The default storage engine after MySQL 5.5 is InnoDB.
- 【Utils】ServletUtil
- MeterSphere--开源持续测试平台
- LeetCode 0143. 重排链表
- MVC model: calendar system
猜你喜欢

These three online PS tools should be tried

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

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能

Career planning of Software Test Engineer

How to effectively conduct the review meeting (Part 1)?

解决uniapp微信小程序canvas不能引入字体的问题

As a programmer, how to manage time efficiently?

Install mysql5.7.36 in CentOS

Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence

MeterSphere--开源持续测试平台
随机推荐
What is gossip (E-Net gossip)
[ecmascript6] set and map
Metersphere -- Open Source continuous testing platform
Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
Multithreading and high concurrency (III) -- source code analysis AQS principle
Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
多级缓存方案
草料二维码--在线二维码生成器
【Utils】ServletUtil
超好用的手机录屏软件推荐
83. (cesium home) how the cesium example works
Leetcode 0143. rearrange linked list
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
LeetCode 1331.数组序号转换
线程阻塞的三种情况。
Several solutions to spanning
这3款在线PS工具,得试试
Read how to deploy highly available k3s with external database
Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)