当前位置:网站首页>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;
}
};
边栏推荐
- 【SQL注入】联合查询(最简单的注入方法)
- 雇佣收银员(差分约束)
- Market status and development prospect prediction of the global fire hose industry in 2022
- Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
- The 19th Zhejiang I. barbecue
- The least operation of leetcode simple problem makes the array increment
- Market status and development prospect prediction of the global fire extinguisher industry in 2022
- UiPath实战(08) - 选取器(Selector)
- 【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
- Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
猜你喜欢
Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
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
逆袭大学生的职业规划
MPM model and ab pressure test
论文阅读_清华ERNIE
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
Flutter monitors volume to realize waveform visualization of audio
Symbol of array element product of leetcode simple problem
M1 Pro install redis
随机推荐
[SQL injection point] location and judgment of the injection point
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
Messy change of mouse style in win system
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
Market status and development prospect prediction of the global fire extinguisher industry in 2022
Notes | numpy-07 Slice and index
Notes | numpy-10 Iterative array
编译GCC遇到的“pthread.h” not found问题
Distinguish between releases and snapshots in nexus private library
Market status and development prospects of the global automatic tea picker industry in 2022
1114 family property (25 points)
论文阅读_ICD编码_MSMN
The least operation of leetcode simple problem makes the array increment
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
String matching: find a substring in a string
Flutter monitors volume to realize waveform visualization of audio
Oracle SQL table data loss
2022-02-12 daily clock in: problem fine brush
Handling record of electric skateboard detained by traffic police