当前位置:网站首页>【LeetCode】1331. 数组序号转换
【LeetCode】1331. 数组序号转换
2022-07-28 13:18:00 【pass night】
题目
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
序号代表了一个元素有多大。序号编号的规则如下:
- 序号从 1 开始编号。
- 一个元素越大,那么序号越大。如果两个元素相等,那么它们的序号相同。
- 每个数字的序号都应该尽可能地小。
示例 1:
输入:arr = [40,10,20,30]
输出:[4,1,2,3]
解释:40 是最大的元素。 10 是最小的元素。 20 是第二小的数字。 30 是第三小的数字。
示例 2:
输入:arr = [100,100,100]
输出:[1,1,1]
解释:所有元素有相同的序号。
示例 3:
输入:arr = [37,12,28,9,100,56,80,5,12]
输出:[5,3,4,2,8,6,7,1,3]
提示:
0 <= arr.length <= 105-109 <= arr[i] <= 109
思路
- 将数组排序,将排序后的下标作为数组的位次
- 遇到重复的数字位次不增加
代码
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
复杂度
- 时间复杂度: O ( n log n ) O(n\log n) O(nlogn)
- 空间复杂度: O ( n ) O(n) O(n)
边栏推荐
- Slam thesis collection
- a标签_文件下载(download属性)
- 走进音视频的世界——FLV视频封装格式
- IP黑白名单
- 安全保障基于软件全生命周期-PSP应用
- RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
- Qt5 development from introduction to mastery -- the first overview
- Websocket chat
- 利用反射构建一棵菜单生成树
- Dojp1520 gate jumping problem solution
猜你喜欢
随机推荐
牛客多校-Link with Level Edito I-(线性dp)
7.27 simulation summary
Poj3268 shortest path solution
什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
Literature reading (245) roller
掌握闭包,夯实基本功
了解虚拟列表背后原理,轻松实现虚拟列表
jenkins
How to effectively conduct the review meeting (Part 1)?
解决跨越的几种方案
安全保障基于软件全生命周期-Istio的认证机制
Security assurance is based on software life cycle -psp application
Vite configuring path aliases in the project
regular expression
Deploy application delivery services in kubernetes (Part 1)
Intersectionobserver
【翻译】盐业公司来Linkerd公司是为了负载平衡,留下来是为了效率、可靠性和性能。...
Diablo 4 ps4/ps5 beta has been added to the Playstation database
第六章 支持向量机
IP黑白名单








