当前位置:网站首页>leetcode406. Rebuild the queue based on height
leetcode406. Rebuild the queue based on height
2022-07-03 04:58:00 【Day by day, the dish chicken Jie!!】
One : You don't have to turn against the wind , But please be born to the sun
Two : subject
3、 ... and : Upper code
class Solution {
public:
/** Analyze the meaning of the topic : This gives people In the array , We need according to its elements people[i] = [hi,ki]; And then according to its ki To indicate greater than hi The number of tall people , To sort . Ideas :1. First we have to sort , So which key value do we sort by ? 1>: If the k Value sorting , It won't work people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] If the k If it's worth it Then [4,4] will At the end , It violates its front only 4 A rule that height is higher than height 2>: Then press h sorted From high to low If we meet people of similar height, we need to k It's worth putting the small one in front ( because k It means before The height of the face is higher ), When the order is arranged in this way, the rest is to insert . After arranging the order : [[7,0],[7,1],[6,1],[5,0],[5,2],[4,4]] We take [5,2] give an example ,[5,2] The front one must be better than 5 higher , Then we can insert it in the subscript 2 The location of , Because the front Subscript to be 0,1 Is taller than his height */
static bool cmp(const vector<int> &v1,const vector<int> &v2) {
if(v1[0] == v2[0]) return v1[1] < v2[1];// When the body height is the same , Let's compare k value
return v1[0] > v2[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(),people.end(),cmp);
vector<vector<int> >ans;
for(int i = 0; i < people.size(); i++) {
int postion = people[i][1];// Get the subscript to insert
ans.insert(ans.begin()+postion,people[i]);
}
return ans;
}
};
边栏推荐
- Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
- ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
- 1119 pre- and post order traversals (30 points)
- 论文阅读_中文NLP_ELECTRA
- 雇佣收银员(差分约束)
- M1 Pro install redis
- [luatos sensor] 1 light sensing bh1750
- [develop wechat applet local storage with uni app]
- String matching: find a substring in a string
- Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
猜你喜欢
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
RT thread flow notes I startup, schedule, thread
Compile and decompile GCC common instructions
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
论文阅读_清华ERNIE
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
Kept hot standby and haproxy
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
随机推荐
Market status and development prospects of the global IOT active infrared sensor industry in 2022
Number of 1 in binary (simple difficulty)
并发操作-内存交互操作
MC Layer Target
I stepped on a foundation pit today
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
112 stucked keyboard (20 points)
RT thread flow notes I startup, schedule, thread
[luatos sensor] 1 light sensing bh1750
【SQL注入】联合查询(最简单的注入方法)
The least operation of leetcode simple problem makes the array increment
Current market situation and development prospect forecast of the global fire boots industry in 2022
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
Network security textual research recommendation
论文阅读_中文医疗模型_ eHealth
联发科技2023届提前批IC笔试(题目)
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
Learning record of arouter principle
Concurrent operation memory interaction
1106 lowest price in supply chain (25 points)