当前位置:网站首页>【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)
边栏推荐
- [lvgl events] Application of events on different components (I)
- 修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
- 多级缓存方案
- Several efficient APIs commonly used in inventory operation URL
- Product Manager: job responsibility table
- Qt5开发从入门到精通——第一篇概述
- Understanding of stack and practical application scenarios
- QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
- Implementation of StrCmp, strstr, memcpy, memmove
- Dojp1520 gate jumping problem solution
猜你喜欢

多级缓存方案

IP黑白名单

多线程与高并发(三)—— 源码解析 AQS 原理

Security assurance is based on software life cycle -psp application

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

记一次COOKIE的伪造登录

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

Clickhouse架构与设计

文献阅读(245)Roller

MySQL开发技巧——视图
随机推荐
【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)
Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
深度学习基础----GNN谱域和空域 (不断完善更新积累)
【LVGL事件(Events)】事件代码
盘点操作URL中常用的几个高效API
Product Manager: job responsibility table
安全保障基于软件全生命周期-NetworkPolicy应用
数据库系统概论(第5版)补充习题——第一章 绪论
Deploy application delivery services in kubernetes (Part 1)
Power amplifier and matching network learning
一文读懂如何部署具有外部数据库的高可用 K3s
【Utils】FastDFS工具类
leetcode(442)数组中重复的数据
Verification code brute force cracking test [easy to understand]
Istio四之故障注入和链路追踪
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
【Utils】CookieUtil
Three cases of thread blocking.
83. (cesium home) how the cesium example works
论文研读--Masked Generative Distillation