当前位置:网站首页>[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)
边栏推荐
- The default storage engine after MySQL 5.5 is InnoDB.
- Introduction to database system (5th Edition) supplementary exercises - Chapter 1 Introduction
- 软件测试的发展与定义
- Clickhouse distributed cluster construction
- Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence
- Read how to deploy highly available k3s with external database
- [ecmascript6] class
- 如何有效进行回顾会议(上)?
- Clickhouse架构与设计
- Three methods to disassemble the rotation array
猜你喜欢

Slam thesis collection

Jmeter安装教程及登录增加token

Clickhouse架构与设计

Development and definition of software testing

RSA用私钥加密数据公钥解密数据(不是签名验证过程)

用友BIP CRM新品发布,赋能大中型企业营销增长

As a programmer, how to manage time efficiently?

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

每日一题——奖学金

Security assurance is based on software life cycle - networkpolicy application
随机推荐
Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
【Utils】JsonUtil
用友BIP CRM新品发布,赋能大中型企业营销增长
Security assurance is based on software life cycle - networkpolicy application
【Utils】JsonUtil
qml 图片预览
天气这么热太阳能发电不得起飞喽啊?喽啊个头……
Docker deploys Mysql to realize remote connection [easy to understand]
如何有效进行回顾会议(上)?
走进音视频的世界——FLV视频封装格式
Thrift 序列化协议浅析
Redis sentinel mechanism
【Utils】CookieUtil
Mobile phone scrolling screenshot software recommendation
[ecmascript6] proxy and reflection
7.27模拟赛总结
成为绿色数据中心新样板,东莞华为云数据中心是怎样炼成的?
Jmeter安装教程及登录增加token
Multithreading and high concurrency (III) -- source code analysis AQS principle
bgp实验