diff --git a/README.md b/README.md index c610aad..8a24f74 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 | 400 |[Nth Digit](https://leetcode.com/problems/nth-digit)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/400.%20Nth%20Digit.md)|Medium| | | 404 |[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/404.%20Sum%20of%20Left%20Leaves.md)|Easy| | | 405 |[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/405.%20Convert%20a%20Number%20to%20Hexadecimal.md)|Easy| | +| 406 |[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)|[C++](solutions/406.%20Queue%20Reconstruction%20by%20Height.md)|Medium| | | 409 |[Longest Palindrome](https://leetcode.com/problems/longest-palindrome)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/409.%20Longest%20Palindrome.md)|Easy| | | 412 |[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[C++](solutions/412.%20Fizz%20Buzz.md)|Easy| | | 414 |[Third Maximum Number](https://leetcode.com/problems/third-maximum-number)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/414.%20Third%20Maximum%20Number.md)|Easy| | diff --git a/solutions/406. Queue Reconstruction by Height.md b/solutions/406. Queue Reconstruction by Height.md new file mode 100644 index 0000000..afcca36 --- /dev/null +++ b/solutions/406. Queue Reconstruction by Height.md @@ -0,0 +1,63 @@ +# [406. Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) + +# 思路 + +有这样一个队列,队列中的每个元素是一个pair,分别为一个人的身高和前面身高不低于当前身高的人的个数,现在这个队列被随机打乱了,让我们恢复这个队列。 + +## 思路一 + +假设我们现在已有了一个满足题意的队列`q`,然后新来了一个人`p`且这个人的身高小于(或等于)该队列中的所有身高,那我们应该怎样将新来的这个人插入到原队列呢?很简单,应该将这个人插入到位置`p[1]`处,即`q.insert(q.begin()+p[1], p)`; + +所以我们可以从一个空队列开始不断插入所有人,为了满足上面的条件“这个人的身高小于(或等于)该队列中的所有身高”,我们应该先对所有人进行排序,身高高的排前面,身高一样的话,则第二个数小的排前面。然后新建一个空的数组,遍历之前排好序的数组,然后根据每个元素的第二个数字,将其插入到 res 数组中对应的位置。 + +由于数组的插入是O(n)的,所以总的时间复杂度为O(n^2) + +## 思路二 + +思路一开辟了外的数组,其实我们可以在元素组上进行插入。还是先排序,然后从前往后遍历,将当前元素插入到前面的合适位置。这里的插入需要我们手动将需要移动的元素往后移一步。 + +复杂度同思路一,但是实际测试要快不少。 + + +# C++ +## 思路一 +``` C++ +class Solution { +public: + vector> reconstructQueue(vector>& people) { + sort(people.begin(), people.end(), [](const vector&a, const vector&b){ + return a[0] != b[0] ? a[0] > b[0] : a[1] < b[1]; + }); + vector>res; + for(auto p: people) + res.insert(res.begin() + p[1], p); + + return res; + } +}; +``` + + +## 思路二 +``` C++ +class Solution { +public: + vector> reconstructQueue(vector>& people) { + sort(people.begin(), people.end(), [](const vector&a, const vector&b){ + return a[0] != b[0] ? a[0] > b[0] : a[1] < b[1]; + }); + + int h, k; + for(int i = 1; i < people.size(); i++){ + h = people[i][0]; k = people[i][1]; + for(int j = i; j > k; j--){ + people[j][0] = people[j-1][0]; + people[j][1] = people[j-1][1]; + } + people[k][0] = h; people[k][1] = k; + } + + return people; + } +}; +``` \ No newline at end of file