Skip to content

Commit dce82fb

Browse files
committed
0406 solved.
1 parent 369524d commit dce82fb

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0406)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0406 main.cpp)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Source : https://leetcode.com/problems/queue-reconstruction-by-height/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-29
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Greedy
13+
/// Time Complexity: O(nlogn + n^2)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
18+
19+
sort(people.begin(), people.end(),
20+
[](const vector<int>& p1, const vector<int>& p2){
21+
if(p1[0] != p2[0]) return p1[0] > p2[0];
22+
return p1[1] < p2[1];
23+
});
24+
25+
vector<vector<int>> res = {people[0]};
26+
for(int i = 1; i < people.size(); i ++){
27+
28+
int j = 0, cnt = 0;
29+
for(; j < (int)res.size() && cnt < people[i][1]; j ++){
30+
if(res[j][0] >= people[i][0]) cnt ++;
31+
}
32+
33+
auto iter = res.begin() + j;
34+
res.insert(iter, people[i]);
35+
}
36+
return res;
37+
}
38+
};
39+
40+
41+
void print_vec(const vector<vector<int>>& v){
42+
for(const vector<int>& p: v)
43+
cout << '(' << p[0] << ',' << p[1] << ')';
44+
cout << '\n';
45+
}
46+
47+
int main() {
48+
49+
vector<vector<int>> people1 = {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}};
50+
print_vec(Solution().reconstructQueue(people1));
51+
52+
return 0;
53+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
439439
| 403 | [Frog Jump](https://leetcode.com/problems/Frog-Jump/) | [solution](https://leetcode.com/problems/Frog-Jump/)<br/>[缺:dp] | [C++](0001-0500/0403-Frog-Jump/cpp-0403/) | | |
440440
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [] | [C++](0001-0500/0404-Sum-of-Left-Leaves/cpp-0404/) | | |
441441
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [] | [C++](0001-0500/0405-Convert-a-Number-to-Hexadecimal/cpp-0405/) | | |
442-
| | | | | | |
442+
| 406 | [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [] | [C++](0001-0500/0406-Queue-Reconstruction-by-Height/cpp-0406/) | | |
443443
| 407 | [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) | [] | [C++](0001-0500/0407-Trapping-Rain-Water-II/cpp-0407/) | | |
444444
| | | | | | |
445445
| 410 | [Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) | [solution](https://leetcode.com/problems/split-array-largest-sum/solution/) [题解](https://leetcode-cn.com/problems/split-array-largest-sum/solution/fen-ge-shu-zu-de-zui-da-zhi-by-leetcode/) | [C++](0001-0500/410-Split-Array-Largest-Sum/cpp-410/) | | |

0 commit comments

Comments
 (0)