当前位置:网站首页>365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
2022-07-29 05:26:00 【ShowM3TheCode】
The idea of discretization
This article Well said , The core is to use hash table to reorder
1331. Array number conversion

Code implementation ( Self solution )
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> other = arr;
unordered_map<int, int> _map;
sort(other.begin(), other.end());
for (int i = 0; i < other.size(); i++) {
if (!_map.count(other[i])) {
_map[other[i]] = _map.size() + 1;
}
}
for (int& num : arr) {
num = _map[num];
}
return arr;
}
};
506. Relative rank

Code implementation ( Self solution )
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
vector<int> ref = score;
unordered_map<int, int> _map;
vector<string> ans;
sort(ref.begin(), ref.end(), greater<int>());
for (int i = 0; i < ref.size(); i++) {
_map[ref[i]] = _map.size() + 1;
}
for (int candidate : score) {
switch(_map[candidate]) {
case 1:
ans.push_back("Gold Medal");
break;
case 2:
ans.push_back("Silver Medal");
break;
case 3:
ans.push_back("Bronze Medal");
break;
default:
ans.push_back(to_string(_map[candidate]));
}
}
return ans;
}
};
边栏推荐
猜你喜欢

Qml类型:MouseArea

哈夫曼树以及哈夫曼编码在文件压缩上的应用

QML control: combobox

C语言 一级指针

Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories

Best practices of JD cloud Distributed Link Tracking in financial scenarios

京东云分布式链路追踪在金融场景的最佳实践

This article takes you to understand the implementation of surround notification @around and final notification @after

京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎

MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习
随机推荐
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
预约中,2022京东云产业融合新品发布会线上开启
The latest tank battle 2022 full development notes-1
小鲁客栈---预告篇
The latest tank battle 2022 - full development notes-3
Webrtc audio anti weak network technology (Part 2)
时间复杂度和空间复杂度
C语言求字符串的长度
CryEngine技术
webgl1.0下texture2D和texture2DProj区别
Cmu15-213 malloc lab experiment record
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
CMU15-213 Shell Lab实验记录
R & D efficiency | analysis of kubernetes' core technology and Devops' landing experience
Xiaobai high salary shortcut Qt development game Snake
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
vs2019编译cryengine失败问题处理
321,京东言犀×NLPCC 2022挑战赛开赛!
千人规模互联网公司研发效能成功之路