Skip to content

Commit 7fb99ce

Browse files
committed
New Problem Solution "Queue Reconstruction by Height"
1 parent 0e3f0e2 commit 7fb99ce

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [C++](./algorithms/cpp/queueReconstructionByHeight/QueueReconstructionByHeight.cpp)|Medium|
1112
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [C++](./algorithms/cpp/convertANumberToHexadecimal/ConvertANumberToHexadecimal.cpp)|Easy|
1213
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C++](./algorithms/cpp/sumOfLeftLeaves/SumOfLeftLeaves.cpp)|Easy|
1314
|403|[Frog Jump](https://leetcode.com/problems/frog-jump/) | [C++](./algorithms/cpp/frogJump/FrogJump.cpp)|Hard|
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Source : https://leetcode.com/problems/queue-reconstruction-by-height/
2+
// Author : Hao Chen
3+
// Date : 2016-11-12
4+
5+
/***************************************************************************************
6+
*
7+
* Suppose you have a random list of people standing in a queue. Each person is
8+
* described by a pair of integers (h, k), where h is the height of the person and k is
9+
* the number of people in front of this person who have a height greater than or equal
10+
* to h. Write an algorithm to reconstruct the queue.
11+
*
12+
* Note:
13+
* The number of people is less than 1,100.
14+
*
15+
* Example
16+
*
17+
* Input:
18+
* [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
19+
*
20+
* Output:
21+
* [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
22+
***************************************************************************************/
23+
24+
class Solution {
25+
26+
public:
27+
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
28+
//sort function
29+
auto comp = [](const pair<int, int>& p1, const pair<int, int>& p2)
30+
{ return p1.first == p2.first ? p1.second < p2.second : p1.first > p2.first; };
31+
//sort the people with their height with descending order
32+
// and if the height is same then sort by K with ascending order
33+
sort(people.begin(), people.end(), comp);
34+
35+
// For example:
36+
// Original Queue: [7,0], [4,4], [7,1], [5,0], [6,1], [5,2]
37+
// Sorted Queue: [7,0], [7,1], [6,1], [5,0], [5,2], [4,4]
38+
39+
40+
// Why do we need to sort like this?
41+
//
42+
// ** The position of shorter people is ZERO impacted with higher people. **
43+
//
44+
// and, the shortest people has no impacts to all of people. we can simpley insert it to the Kth position
45+
//
46+
// So, we sorted the people from highest to the shortest, then when we insert the people to another array,
47+
//
48+
// we always can guarantee the people is going to be inserted has nothing to do with the people has been inserted.
49+
//
50+
// Let's continue the about example above
51+
//
52+
// [7,0] => [] then [7,0]
53+
// [7,1] => [7,0] then [7,0], [7,1]
54+
// [6,1] => [7,0], [7,1] then [7,0], [6,1], [7,1]
55+
// [5,0] => [7,0], [6,1], [7,1] then [5,0], [7,0], [6,1], [7,1]
56+
// [5,2] => [5,0], [7,0], [6,1], [7,1] then [5,0], [7,0], [5,2], [6,1], [7,1]
57+
// [4,4] => [5,0], [7,0], [5,2], [6,1], [7,1] then [5,0], [7,0], [5,2], [6,1], [4,4], [7,1]
58+
//
59+
// We alway can see, the people is going to be inserted has NO IMPACT with the current people.
60+
//
61+
// [6,1] => [7,0], [7,1]
62+
//
63+
// Whatever the people[6,1] placed, it has nothing to do with the people [7,0] [7,1],
64+
// So, we can just insert the people to the place he like - the `Kth` place.
65+
//
66+
//
67+
vector<pair<int, int>> res;
68+
for (auto& p : people) {
69+
res.insert(res.begin() + p.second, p);
70+
}
71+
return res;
72+
}
73+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy