id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool compare (vector<int>& a, vector<int>& b) {\n if (a[0] != b[0]) return a[0] < b[0];\n return a[0] < b[0];\n }\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n = people.size();\n sort(people.begin(), people.end(), compare);\n vector<vector<int>> ans(n, {INT_MAX, 0});\n\n for (vector<int>& person: people) {\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n if (ans[i][0] >= person[0]) cnt++;\n if (cnt > person[1]) {\n ans[i] = person;\n break;\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "14900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n std::vector<std::vector<int>> ans(people.size(), {-1, 1});\n\n std::sort(people.begin(), people.end());\n\n int height = -1, head = 0, tail = people.size(), cur = 0, i = 0;\n for (auto const& p: people) {\n if (p[0] > height) {\n height = p[0];\n cur = 0;\n i = head;\n }\n\n int k = p[1];\n while (k > cur) {\n ++cur;\n i += ans[i][1];\n }\n\n int prev = ans[i][0], next = ans[i][1];\n\n if (i == head)\n head += next;\n else\n ans[i + prev][1] += next;\n\n if (i + next < tail)\n ans[i + next][0] += prev;\n\n ans[i][0] = p[0];\n ans[i][1] = p[1];\n\n ++cur;\n i += next;\n }\n\n return ans;\n }\n};",
"memory": "15000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int peopleSize = people.size();\n vector<vector<int>> ans(peopleSize);\n sort(people.begin(), people.end());\n for(int index=0;index<people.size();index++){\n int greaterHeightPeopleCount = people[index][1];\n int start=0;\n if(greaterHeightPeopleCount){\n for(;start<people.size();start++){\n if(ans[start].size()==0 || ans[start][0]==people[index][0]){\n greaterHeightPeopleCount--;\n if(greaterHeightPeopleCount==0){\n break;\n }\n }\n }\n }else{\n start=-1;\n }\n for(int i=start+1;i<people.size();i++){\n if(ans[i].size()==0){\n ans[i]=people[index];\n break;\n }\n }\n }\n return ans;\n }\n};",
"memory": "15100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // Comparator function\n static bool compare(const vector<int>& a, const vector<int>& b) {\n if (a[0] == b[0]) {\n return a[1] < b[1]; \n }\n return a[0] > b[0]; \n }\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), compare);\n vector<vector<int>> result;\n for (int i = 0; i < people.size(); i++) {\n result.insert(result.begin() + people[i][1], people[i]);\n }\n\n return result;\n }\n};\n",
"memory": "15200"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n auto cmp = [&](const vector<int>& a, const vector<int>& b) {\n return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0];\n };\n sort(people.begin(), people.end(), cmp);\n\n //insert\n for (int i = 0; i < people.size(); ++i) {\n //cout << people[i][0] << \" \" << people[i][1] << \"\\n\";\n int v = people[i][1];\n for (int j = i; j > v; --j) {\n swap(people[j], people[j-1]);\n }\n }\n return people;\n }\n};",
"memory": "15200"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class BIT {\nprivate:\n int size;\n vector<int> bit;\n \npublic:\n BIT (int n) : size(n), bit(n, 0) {\n for (int i = 0; i < size; i++) {\n add(i, 1);\n }\n }\n\n void add(int idx, int val) {\n for (; idx < size; idx |= (idx + 1)) {\n bit[idx] += val;\n }\n }\n\n void unset(int idx) {\n add(idx, -1);\n }\n\n int pref_sum(int idx) {\n int sum = 0;\n for (; idx >= 0; idx = (idx & (idx + 1)) - 1) sum += bit[idx];\n return sum;\n }\n\n int getKthEmptyPosition(int k) {\n int low = 0, high = size - 1;\n while (low < high) {\n int mid = low + (high - low) / 2;\n if (pref_sum(mid) >= k) high = mid;\n else low = mid + 1;\n }\n return low;\n }\n};\n\n\nclass Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end());\n vector<vector<int>> result(people.size());\n\n BIT bit = BIT(people.size());\n int sameCount = 0;\n for (int i = 0; i < people.size(); i++) {\n if (i && people[i][0] == people[i - 1][0]) sameCount++;\n else sameCount = 0;\n \n int idx = bit.getKthEmptyPosition(people[i][1] - sameCount + 1);\n result[idx] = people[i];\n bit.unset(idx);\n }\n\n return result;\n }\n};",
"memory": "15300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n std::sort(people.begin(), people.end());\n vector<vector<int>> result(people.size(), {-1,-1});\n for(const auto& p : people) {\n int i = 0;\n for(int skipped = 0; skipped < p[1] || result[i][0] != -1; ++i){\n if(result[i][0] == -1 || result[i][0] == p[0]) ++skipped;\n }\n result[i] = p;\n }\n return result;\n }\n};",
"memory": "15300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 0 | {
"code": "struct lessThanComp{\n bool operator()(const vector<int>&a1 , const vector<int> &a2){\n if(a1[0] == a2[0]){\n return a1[1] < a2[1];\n }\n else{\n return a1[0] < a2[0];\n }\n }\n};\nclass Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n = people.size();\n vector<vector<int>> result(n , vector<int>());\n sort(people.begin() , people.end() , lessThanComp());\n // for(auto x : people)cout<<x[0]<<\" \"<<x[1]<<endl;\n for(int i = 0 ; i < n ; i++)\n {\n int count = people[i][1];\n int he = people[i][0];\n for(int j = 0 ; j < n ; j++)\n {\n if(result[j].size()==0 && count==0)\n {\n result[j] = people[i];\n break;\n }\n else if(result[j].size()==0 || result[j][0]>=he)count--;\n }\n }\n return result;\n }\n};",
"memory": "15400"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 1 | {
"code": "#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n // Sort people by descending height and then by ascending k values\n std::sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {\n if (a[0] == b[0])\n return a[1] < b[1];\n return a[0] > b[0];\n });\n\n vector<vector<int>> queue;\n // Insert each person into the queue based on their k value\n for (const auto& person : people) {\n queue.insert(queue.begin() + person[1], person);\n }\n\n return queue;\n }\n};",
"memory": "15500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 1 | {
"code": "class Solution {\n int n;\n int getNext(int val){\n return val + (val&-val);\n }\n\n int getParent(int val){\n return val - (val&-val);\n }\n\n void update(vector<int>& tree, int index, int val){\n if(index>=tree.size()){\n return;\n }\n tree[index]+=val;\n update(tree, getNext(index), val);\n }\n\n void buildFenwickTree( vector <int> &t)\n {\n for(int i=1;i<n;i++)\n {\n int p= i + (i&-i);\n if(p<=n)\n t[p]+=t[i]; \n }\n }\n\n void buildTree(vector<int>& tree){\n for(int i=1;i<=n;i++){\n update(tree, i, 1);\n }\n }\n\n int emptyTillPos(vector<int>& tree, int index){\n int sum=0;\n while(index>0){\n sum+=tree[index];\n index=getParent(index);\n }\n return sum;\n }\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n n=people.size();\n vector<int> tree(n+1, 0);\n buildTree(tree);\n vector<int> tree2(n+1, 1);\n buildFenwickTree(tree2);\n for(auto i: tree){\n cout<<i<<\" \";\n }\n cout<<endl;\n for(auto i: tree2){\n cout<<i<<\" \";\n }\n cout<<endl;\n auto compareFunction = [](vector<int>& p1, vector<int>& p2){\n if(p1[0]!=p2[0]){\n return p1[0]<p2[0];\n }\n return p1[1]>p2[1];\n };\n sort(people.begin(), people.end(), compareFunction);\n vector<vector<int>> ans(n);\n for(int i=0;i<n;i++){\n int l=1, r=n, mid;\n while(l<r){\n mid = (l+r)/2; \n if(emptyTillPos(tree, mid)<=people[i][1]){\n l=mid+1;\n }else{\n r=mid;\n }\n }\n update(tree, l, -1);\n ans[l-1]=people[i];\n }\n return ans;\n }\n};",
"memory": "15500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> tree;\n int build(int node, int start, int end) {\n if(start == end) {\n tree[node - 1] = 1;\n return 1;\n }\n int mid = (start + end) / 2;\n int left = build(node * 2, start, mid);\n int right = build(node * 2 + 1, mid + 1, end);\n tree[node - 1] = left + right;\n return left + right;\n }\n\n void update(int node, int start, int end, int idx) {\n if(start == end) {\n tree[node - 1] = 0;\n return;\n }\n int mid = (start + end) / 2;\n if(idx <= mid) {\n update(node * 2, start, mid, idx);\n } else {\n update(node * 2 + 1, mid + 1, end, idx);\n }\n tree[node - 1] -= 1;\n }\n\n int find(int node, int start, int end, int count) {\n if(start == end) {\n return start;\n }\n int mid = (start + end) / 2;\n int left = tree[node * 2 - 1];\n int right = tree[node * 2];\n if(count <= left && left != 0) {\n return find(node * 2, start, mid, count);\n } else {\n return find(node * 2 + 1, mid + 1, end, count - left);\n }\n }\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [](vector<int>& a, vector<int>& b) {\n if(a[0] == b[0]) return a[1] > b[1];\n return a[0] < b[0];\n });\n\n tree.resize(0);\n int size = people.size();\n int tree_size = pow(2, ceil(log2(size)) + 1);\n tree.resize(tree_size);\n build(1, 0, size - 1);\n\n vector<vector<int>> result(size, vector<int>());\n for(int i = 0; i < size; i++) {\n int idx = find(1, 0, size - 1, people[i][1] + 1);\n result[idx] = people[i];\n update(1, 0, size - 1, idx);\n }\n\n return result;\n }\n};",
"memory": "15600"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>> ans;\n // Sort by height in descending order, if same height then by the number of people in ascending order\n sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {\n return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);\n });\n // Insert people in the result vector at the correct position\n for (auto& person : people) {\n ans.insert(ans.begin() + person[1], person);\n }\n return ans;\n }\n};",
"memory": "15700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n = people.size();\n if (n == 1) return people;\n // sort the queue by how many higher people in front\n sort(people.begin(), people.end(), [] (const vector<int>& a, const vector<int>& b) {\n return a[1] < b[1];\n });\n \n vector<vector<int>> q;\n q.push_back(people[0]);\n \n for (int i=1; i<n; i++) {\n // the number of higher people in front\n int N_higher = people[i][1];\n // current found higher people in the queue\n int cur_N_higher = 0;\n // the position where people[i] should be placed\n int pos = i;\n for (int j=0; j<i; j++) {\n // start from the front of the queue\n // if a higher people is found, cur_N_higher++\n if (q[j][0] >= people[i][0]) cur_N_higher++;\n // if cur_N_higher > N_higher, it is the place where people[i] should be placed\n // if this condition never occur, people[i] should be placed at the end of the current queue\n if (cur_N_higher > N_higher) {\n pos = j;\n break;\n }\n }\n // place the people[i] with the variable pos\n q.emplace(q.begin()+pos, people[i]);\n }\n\n return q;\n }\n};",
"memory": "15700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>> ans;\n // Sort by height in descending order, if same height then by the number of people in ascending order\n sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {\n return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);\n });\n // Insert people in the result vector at the correct position\n for (auto& person : people) {\n ans.insert(ans.begin() + person[1], person);\n }\n return ans;\n }\n};",
"memory": "15800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>> ans;\n // Sort by height in descending order, if same height then by the number of people in ascending order\n sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {\n return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);\n });\n // Insert people in the result vector at the correct position\n for (auto& person : people) {\n ans.insert(ans.begin() + person[1], person);\n }\n return ans;\n }\n};",
"memory": "15800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n static bool compare(vector<int> &a,vector<int> &b){\n if(a[0]==b[0])\n return a[1]<b[1];\n return a[0]>b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>> ans;\n sort(people.begin(),people.end(),compare);\n\n for(auto it:people){\n ans.insert(ans.begin()+it[1],it);\n }\n return ans;\n \n }\n};",
"memory": "15900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\n //[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]\n // [70][71][61][50][52][44]\n\n // vAns = 50 70 52 61 44 71 \n\n static bool customSort(std::vector<int>& v1, std::vector<int>& v2)\n {\n if (v1[0] > v2[0])\n return true;\n\n if (v1[0]== v2[0] && v1[1] < v2[1])\n return true;\n\n return false;\n }\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n //1. sort vectors by no tallest person infront of current person\n //2. if heights are same then lowest position will go in fisrt\n\n std::sort(people.begin(), people.end(), customSort);\n std::vector<vector<int>> vAns;\n\n for (auto person : people)\n {\n vAns.insert(vAns.begin() + person[1], person);\n }\n\n return vAns;\n } \n};",
"memory": "15900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [](vector<int> const& a, vector<int> const& b) {\n if (a[0] != b[0]) { return a[0] > b[0]; }\n return a[1] < b[1];\n});\n\nusing entry = pair<int, int>;\nlist<entry> buffer;\nfor (auto const& p : people) {\n int count = p[1];\n auto iter = buffer.begin();\n for (int i = 0; i < count; ++i) { ++iter; }\n buffer.insert(iter, { p[0], p[1] });\n}\n\nvector<vector<int>> result;\nfor (auto iter = buffer.begin(); iter != buffer.end(); ++iter) {\n result.push_back({ (*iter).first, (*iter).second });\n}\nreturn result;\n }\n};",
"memory": "16300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;\n for (auto pair: people){\n pq.push({pair[0], pair[1]});\n }\n int n=people.size();\n vector<vector<int>> result(n, vector<int>{});\n for (unsigned int i = 0; i < n; i++){\n pair<int, int> top=pq.top();\n pq.pop();\n int num_before=top.second;\n for (unsigned int j = 0; j < n; j++){\n if (result[j].size() == 0){\n if (num_before==0){\n result[j]={top.first, top.second};\n break;\n }\n num_before--;\n }\n else if (result[j][0] == top.first){\n num_before--;\n }\n }\n }\n return result;\n }\n};",
"memory": "16400"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>> temp = people;\n vector<vector<int>> result;\n\n auto comp = [](pair<int, int> a, pair<int, int> b) {\n return a.first > b.first;\n };\n priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> q(comp);\n\n for(int i = 0; i < people.size(); i++) {\n if(people[i][1] == 0) {\n q.push({people[i][0], i});\n }\n }\n\n while(!q.empty()) {\n auto t = q.top();\n q.pop();\n result.push_back(people[t.second]);\n for(int i = 0; i < temp.size(); i++) {\n if(temp[i][0] <= t.first) {\n temp[i][1]--;\n if(temp[i][1] == 0) {\n q.push({temp[i][0], i});\n }\n }\n }\n }\n\n return result;\n }\n};",
"memory": "16500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "// vector动态数组insert的时间复杂度高\n// 用底层为链表实现的list容器更快\nclass Solution {\npublic:\n static bool cmp(vector<int>& a, vector<int>& b) {\n if (a[0] == b[0]) return a[1] < b[1]; // 身高相等,前面人少的优先\n return a[0] > b[0]; // 高的优先\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n list<vector<int>> result;\n sort(people.begin(), people.end(), cmp);\n for (int i = 0; i < people.size(); i++) {\n int position = people[i][1];\n list<vector<int>>::iterator it = result.begin();\n while (position--) {\n it++; // 更新迭代器位置\n }\n result.insert(it, people[i]);\n }\n return vector<vector<int>>(result.begin(), result.end());\n }\n};",
"memory": "16600"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "#include <list>\n\nclass Solution {\n #pragma GCC optimize(\"O3\")\n\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) \n {\n sort(people.begin(), people.end(), [] (const vector<int>& a, const vector<int>& b) \n {\n if (a[0] == b[0])\n return a[1] < b[1];\n return a[0] > b[0];\n });\n\n list<vector<int>> ansList;\n \n for (const auto& person : people) \n {\n auto it = next(ansList.begin(), person[1]);\n ansList.insert(it, person);\n }\n\n vector<vector<int>> ans(ansList.begin(), ansList.end());\n return ans;\n }\n};\n",
"memory": "16700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "bool cmp(vector<int>& personA, vector<int>& personB){\n if(personA[0] == personB[0])return personA[1] > personB[1];\n else return personA[0] < personB[0];\n};\n\nint getNthIndex(\n unordered_map<int, int>& takenPositions,\n int n, \n int k\n){\n int count = 0;\n for(int position = 0; position < n; position++){\n if(takenPositions.find(position) == takenPositions.end()){\n count++;\n if(count == k)return position;\n }\n }\n return -1;\n}\n\nclass Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>> result(people.size());\n unordered_map<int, int> takenPositions;\n sort(people.begin(), people.end(), cmp);\n for(auto person: people){\n int k = person[1]+1;\n int allocatedPosition = getNthIndex(takenPositions, people.size(), k);\n takenPositions[allocatedPosition] = 1;\n result[allocatedPosition] = person;\n }\n return result;\n }\n};",
"memory": "16700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>>v;\n vector<int>t;\n int gI(int i, int sp, int st, int en){\n if(st == en) return st;\n else{\n int mid = (st+en)/2;\n int w = mid-st+1;\n int l = t[2*i+1];\n int r = t[2*(i+1)];\n\n if(w - l>= sp)\n return gI(2*i+1, sp, st, mid);\n else \n return gI(2*i+2, sp - (w-l), mid+1, en);\n }\n }\n\n void update(int k, int i, int st, int en){\n if(st == en) t[i] = 1;\n else{\n int mid = (st+en)/2;\n if(k <= mid) update(k, 2*i+1, st, mid);\n else update(k, 2*i+2, mid+1, en);\n\n t[i] = t[2*i+1] + t[2*i+2];\n }\n }\n\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& p) {\n sort(p.begin(), p.end());\n\n int n = p.size();\n t = vector<int>(4*n+1, 0);\n\n vector<vector<int>> v(n);\n map<int,int> mp;\n\n for(int i=0; i<n; i++){\n int sp = p[i][1] - mp[p[i][0]];\n int idx = gI(0, sp+1, 0, n-1);\n v[idx] = p[i];\n\n update(idx, 0, 0, n-1);\n mp[p[i][0]]++;\n }\n\n return v;\n }\n};",
"memory": "16800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n static bool cmp (const vector<int> & A, const vector<int> & B) {\n if (A[0] == B[0]) return A[1] <B[1];\n return A[0] > B[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), cmp);\n list<vector<int>> que;\n //先將身高由高到矮排列,之後由高到矮調整,參考k把人移到第k個位置\n //移了之後,因為前面的人都比他高,所以確保前面有K個人比他高\n //中間牽涉一系列的插入,所以選擇插入效率高的list而不是vector\n int size = people.size();\n for(int i = 0; i < size; i++) {\n int position = people[i][1];\n std::list<vector<int>>::iterator it = que.begin();\n while (position--) {\n it++;\n }\n\n que.insert(it, people[i]);\n }\n vector<vector<int>> ans(que.begin(), que.end());\n return ans;\n }\n};",
"memory": "16900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\n struct comp{ \n template <typename T>\n bool operator()(const T& e1, const T& e2)const{\n if(e1[0]==e2[0]){\n return e1[1] < e2[1];\n }\n return e1[0] > e2[0];\n }\n };\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& ppl) {\n vector<vector<int>> res(ppl.size(),vector<int>());\n priority_queue<vector<int>,vector<vector<int>>,comp> pq;\n for (vector<int> p: ppl){\n pq.push(p);\n }\n vector<int> temp;\n int place;\n while (!pq.empty()){\n temp = pq.top();\n pq.pop();\n place = temp[1];\n for (int i = 0;i<res.size();i++){\n if (place && res[i].size()==0){\n place--;\n continue;\n }\n if (res[i].size()!=0){\n continue;\n }\n res[i] = temp;\n break;\n }\n }\n return res;\n }\n};\n\n\n\n/*\nSo we have all the people and the array shows how many ppl should be in front of them\n[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]\n\n[5,0][7,0] [6,1] [5,2] [4,4] [7,1]\n\n\n[5,0][7,0][6,1][5,2] [4,4][7,1]\n\n\nSo unlike the initial thought that we sort them based on the shortest height first, we sort them based on the tallest first\n\n\n*/",
"memory": "17000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n int n = people.size();\n vector<vector<int>> copy = people;\n vector<vector<int>> res;\n for(int i=0; i<n; i++){\n int height = people[i][0];\n int ahead = people[i][1];\n if(ahead == 0){\n pq.push({height, i});\n people[i][1]--;\n }\n }\n while(!pq.empty()){\n int height = pq.top()[0];\n int index = pq.top()[1];\n res.push_back({height, copy[index][1]});\n pq.pop();\n for(int i=0; i<n; i++){\n if(people[i][0] <= height){\n people[i][1]--;\n if(people[i][1] == 0){\n pq.push({people[i][0], i});\n }\n }\n }\n }\n return res;\n }\n};",
"memory": "17200"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool cmp(vector<int>& a, vector<int>& b){\n if(a[0] == b[0]) return a[1] < b[1];\n else return a[0] > b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), cmp);\n list<vector<int>> ans;\n for(vector<int> v : people){\n int pos = v[1];\n auto it = ans.begin();\n while(pos--) it++;\n ans.insert(it, v);\\\n }\n return vector<vector<int>> (ans.begin(), ans.end());\n }\n};",
"memory": "17300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n static bool comparator(vector<int> &a, vector<int> &b){\n if( a[0] != b[0])\n return a[0] > b[0];\n return a[1] < b[1];\n }\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(), comparator);\n list<vector<int>> li;\n for( auto p : people){\n auto pos = li.begin();\n advance(pos, p[1]); \n li.insert(pos, p); \n }\n return vector<vector<int>>(li.begin(), li.end());;\n }\n};",
"memory": "17400"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct compare{\n bool operator()(vector<int>&a, vector<int>&b){\n if(a[0]==b[0])\n return a[1]>b[1];\n return a[0]<b[0];\n }\n };\n \n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n=people.size();\n priority_queue<vector<int>, vector<vector<int>>, compare> pq;\n vector<vector<int>> ans;\n for(int i=0;i<n;i++){\n pq.push({people[i][0], people[i][1]});\n }\n while(!pq.empty()){\n auto curr=pq.top(); \n ans.insert(ans.begin()+curr[1], curr);\n pq.pop();\n }\n return ans;\n }\n};",
"memory": "17500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool cond(pair<int, int> a, pair<int, int> b) {\n if (a.second != b.second) {\n return a.second < b.second;\n }\n return a.first < b.first;\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<pair<int, int>> v;\n for (auto it : people) {\n v.push_back({it[0], it[1]});\n }\n sort(begin(v), end(v), cond);\n set<pair<int, int>> st{begin(v), end(v)};\n vector<vector<int>> ans;\n vector<int> arr;\n for (auto it : v) {\n if (it.second == 0) {\n arr.push_back(it.first);\n ans.push_back({it.first, it.second});\n st.erase({it.first, it.second});\n break;\n } else {\n break;\n }\n }\n int k=9;\n // for(auto x:st){\n // cout<<x.first<<\" \"<<x.second<<endl;\n // }\n int n=people.size();\n while (st.size()) {\n for (auto it : st) {\n int num = it.first;\n int cnt = it.second;\n int lb = lower_bound(arr.begin(), arr.end(), num) - arr.begin();\n int x = arr.size() - lb;\n if (cnt == x) {\n // cout<<it.first<<\" \"<<it.second<<\" \"<<cnt<<\" \"<<x<<endl;;\n ans.push_back({num,cnt});\n arr.push_back(num);\n sort(arr.begin(),arr.end());\n st.erase({num,cnt});\n break;\n }\n }\n }\n // for(auto it:ans){\n // // cout<<it[0]<<\" \"<<it[1]<<endl;\n // }\n return ans;\n }\n};",
"memory": "17600"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n const int n = people.size();\n sort(people.begin(), people.end(), [](auto &a, auto &b) {\n return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);\n });\n list<vector<int>> output;\n for (auto &v : people) {\n auto it = output.begin();\n advance(it, v[1]);\n output.insert(it, v);\n }\n vector<vector<int>> ans;\n for (auto v : output) {\n ans.push_back(v);\n }\n return ans;\n }\n};",
"memory": "17700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n/*See editorial*/\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n const int n = people.size();\n sort(people.begin(), people.end(), [](auto &a, auto &b) {\n return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);\n });\n list<vector<int>> output;\n for (auto &v : people) {\n auto it = output.begin();\n advance(it, v[1]);\n output.insert(it, v);\n }\n vector<vector<int>> ans;\n for (auto v : output) {\n ans.push_back(v);\n }\n return ans;\n }\n};",
"memory": "17700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> pos_to_ordered_height_map;\n for (const auto& person: people) {\n const int height = person[0];\n const int num_people = person[1]; \n pos_to_ordered_height_map[num_people].insert(height);\n }\n\n // std::cout << \"pos_to_ordered_height_map -> num_people : ordered_height_list\\n\";\n // for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n // std::cout << \"\\t\" << num_people << \": \";\n // for (const int height : ordered_height_list) std::cout << height << \" \";\n // std::cout << \"\\n\";\n // }\n\n vector<vector<int>> recons_queue;\n for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n if (recons_queue.size() == 0) {\n for (const int height : ordered_height_list) recons_queue.push_back({height, num_people});\n // std::cout << \"Initial reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n continue;\n }\n\n for (const int height : ordered_height_list) {\n for (int i=0; i<recons_queue.size(); ++i) {\n // if (recons_queue[i][0] < height && )\n int count = 0;\n while (i<recons_queue.size() && (recons_queue[i][0] < height || (recons_queue[i][0] >= height && count<num_people))) {\n if (i<recons_queue.size() && recons_queue[i][0] >= height) ++count;\n // std::cout << \"\\t\\ti=\" << i << \"\\tcount=\" << count << \"\\n\";\n ++i;\n if (count == num_people) break;\n }\n if (count == num_people && recons_queue[i-1][0] >= height) --i;\n // std::cout << \"\\nrecons_queue.size()=\" << recons_queue.size() << \"\\theight=\" << height << \"\\tnum_people=\" \n // << num_people << \"\\ti=\" << i << \"\\tcount=\" << count << \"\\tnum_people=\" << num_people << \"\\n\";\n if (i == recons_queue.size()) recons_queue.push_back({height, num_people});\n else {\n while (i+1 < recons_queue.size() && recons_queue[i+1][0] < height) ++i;\n if (i == recons_queue.size()-1) recons_queue.push_back({height, num_people});\n else recons_queue.insert(recons_queue.begin()+ i + 1, {height, num_people});\n }\n\n // std::cout << \"Intermediate reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n \n break;\n }\n }\n }\n\n return recons_queue;\n }\n};",
"memory": "17800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> pos_to_ordered_height_map;\n for (const auto& person: people) {\n const int height = person[0];\n const int num_people = person[1]; \n pos_to_ordered_height_map[num_people].insert(height);\n }\n\n vector<vector<int>> recons_queue;\n for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n if (recons_queue.size() == 0) {\n for (const int height : ordered_height_list) recons_queue.push_back({height, num_people});\n continue;\n }\n\n for (const int height : ordered_height_list) {\n for (int i=0; i<recons_queue.size(); ++i) {\n int count = 0;\n while (i<recons_queue.size() && (recons_queue[i][0] < height || (recons_queue[i][0] >= height && count<num_people))) {\n if (i<recons_queue.size() && recons_queue[i][0] >= height) ++count;\n ++i;\n if (count == num_people) break;\n }\n if (count == num_people && recons_queue[i-1][0] >= height) --i;\n \n if (i == recons_queue.size()) recons_queue.push_back({height, num_people});\n else {\n while (i+1 < recons_queue.size() && recons_queue[i+1][0] < height) ++i;\n if (i == recons_queue.size()-1) recons_queue.push_back({height, num_people});\n else recons_queue.insert(recons_queue.begin()+ i + 1, {height, num_people});\n }\n\n break;\n }\n }\n }\n\n return recons_queue;\n }\n};",
"memory": "17900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> pos_to_ordered_height_map;\n for (const auto& person: people) {\n const int height = person[0];\n const int num_people = person[1]; \n pos_to_ordered_height_map[num_people].insert(height);\n }\n\n // std::cout << \"pos_to_ordered_height_map -> num_people : ordered_height_list\\n\";\n // for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n // std::cout << \"\\t\" << num_people << \": \";\n // for (const int height : ordered_height_list) std::cout << height << \" \";\n // std::cout << \"\\n\";\n // }\n\n vector<vector<int>> recons_queue;\n for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n if (recons_queue.size() == 0) {\n for (const int height : ordered_height_list) recons_queue.push_back({height, num_people});\n // std::cout << \"Initial reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n continue;\n }\n\n for (const int height : ordered_height_list) {\n for (int i=0; i<recons_queue.size(); ++i) {\n // if (recons_queue[i][0] < height && )\n int count = 0;\n while (i<recons_queue.size() && (recons_queue[i][0] < height || (recons_queue[i][0] >= height && count<num_people))) {\n if (i<recons_queue.size() && recons_queue[i][0] >= height) ++count;\n // std::cout << \"\\t\\ti=\" << i << \"\\tcount=\" << count << \"\\n\";\n ++i;\n if (count == num_people) break;\n }\n if (count == num_people && recons_queue[i-1][0] >= height) --i;\n // std::cout << \"\\nrecons_queue.size()=\" << recons_queue.size() << \"\\theight=\" << height << \"\\tnum_people=\" \n // << num_people << \"\\ti=\" << i << \"\\tcount=\" << count << \"\\tnum_people=\" << num_people << \"\\n\";\n if (i == recons_queue.size()) recons_queue.push_back({height, num_people});\n else {\n while (i+1 < recons_queue.size() && recons_queue[i+1][0] < height) ++i;\n if (i == recons_queue.size()-1) recons_queue.push_back({height, num_people});\n else recons_queue.insert(recons_queue.begin()+ i + 1, {height, num_people});\n }\n\n // std::cout << \"Intermediate reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n \n break;\n }\n }\n }\n\n return recons_queue;\n }\n};",
"memory": "18000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> pos_to_ordered_height_map;\n for (const auto& person: people) {\n const int height = person[0];\n const int num_people = person[1]; \n pos_to_ordered_height_map[num_people].insert(height);\n }\n\n // std::cout << \"pos_to_ordered_height_map -> num_people : ordered_height_list\\n\";\n // for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n // std::cout << \"\\t\" << num_people << \": \";\n // for (const int height : ordered_height_list) std::cout << height << \" \";\n // std::cout << \"\\n\";\n // }\n\n vector<vector<int>> recons_queue;\n for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n if (recons_queue.size() == 0) {\n for (const int height : ordered_height_list) recons_queue.push_back({height, num_people});\n // std::cout << \"Initial reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n continue;\n }\n\n for (const int height : ordered_height_list) {\n for (int i=0; i<recons_queue.size(); ++i) {\n int count = 0;\n while (i<recons_queue.size() && (recons_queue[i][0] < height || (recons_queue[i][0] >= height && count<num_people))) {\n if (i<recons_queue.size() && recons_queue[i][0] >= height) ++count;\n // std::cout << \"\\n\\t\\ti=\" << i << \"\\tcount=\" << count;\n ++i;\n if (count == num_people) break;\n }\n if (count == num_people && recons_queue[i-1][0] >= height) --i;\n // std::cout << \"\\nrecons_queue.size()=\" << recons_queue.size() << \"\\theight=\" << height << \"\\tnum_people=\" \n // << num_people << \"\\ti=\" << i << \"\\tcount=\" << count << \"\\tnum_people=\" << num_people << \"\\n\";\n if (i == recons_queue.size()) recons_queue.push_back({height, num_people});\n else {\n while (i+1 < recons_queue.size() && recons_queue[i+1][0] < height) ++i;\n if (i == recons_queue.size()-1) recons_queue.push_back({height, num_people});\n else recons_queue.insert(recons_queue.begin()+ i + 1, {height, num_people});\n }\n\n // std::cout << \"Intermediate reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n \n break;\n }\n }\n }\n\n return recons_queue;\n }\n};",
"memory": "18000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> pos_to_ordered_height_map;\n for (const auto& person: people) {\n const int height = person[0];\n const int num_people = person[1]; \n pos_to_ordered_height_map[num_people].insert(height);\n }\n\n // std::cout << \"pos_to_ordered_height_map -> num_people : ordered_height_list\\n\";\n // for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n // std::cout << \"\\t\" << num_people << \": \";\n // for (const int height : ordered_height_list) std::cout << height << \" \";\n // std::cout << \"\\n\";\n // }\n\n vector<vector<int>> recons_queue;\n for (const auto &[num_people, ordered_height_list] : pos_to_ordered_height_map) {\n if (recons_queue.size() == 0) {\n for (const int height : ordered_height_list) recons_queue.push_back({height, num_people});\n // std::cout << \"Initial reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n continue;\n }\n\n for (const int height : ordered_height_list) {\n for (int i=0; i<recons_queue.size(); ++i) {\n int count = 0;\n while (i<recons_queue.size() && (recons_queue[i][0] < height || (recons_queue[i][0] >= height && count<num_people))) {\n if (i<recons_queue.size() && recons_queue[i][0] >= height) ++count;\n // std::cout << \"\\n\\t\\ti=\" << i << \"\\tcount=\" << count;\n ++i;\n if (count == num_people) break;\n }\n if (count == num_people && recons_queue[i-1][0] >= height) --i;\n // std::cout << \"\\nrecons_queue.size()=\" << recons_queue.size() << \"\\theight=\" << height << \"\\tnum_people=\" \n // << num_people << \"\\ti=\" << i << \"\\tcount=\" << count << \"\\tnum_people=\" << num_people << \"\\n\";\n if (i == recons_queue.size()) recons_queue.push_back({height, num_people});\n else {\n while (i+1 < recons_queue.size() && recons_queue[i+1][0] < height) ++i;\n if (i == recons_queue.size()-1) recons_queue.push_back({height, num_people});\n else recons_queue.insert(recons_queue.begin()+ i + 1, {height, num_people});\n }\n\n // std::cout << \"Intermediate reconstruction_queue: \";\n // for (const auto& elem : recons_queue) std::cout << \"[\" << elem[0] << \",\" << elem[1] << \"] \"; \n // std::cout << \"\\n\";\n \n break;\n }\n }\n }\n\n return recons_queue;\n }\n};",
"memory": "18100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n priority_queue<vector<int>> pq;\n for (auto &it: people) {\n pq.push({it[1], it[0]});\n }\n vector<vector<int>> sorted;\n while (!pq.empty()) {\n sorted.push_back({pq.top()[1], pq.top()[0]});\n pq.pop();\n }\n reverse(sorted.begin(), sorted.end());\n vector<vector<int>> ans;\n ans.push_back(sorted[0]);\n int n = sorted.size();\n for (int j = 1; j < n; j++) {\n int h = sorted[j][0];\n int k = sorted[j][1];\n // cout<<h<<\" \"<<k<<\"\\n\";\n int ct = 0;\n int idx = 0;\n for (int i = 0; i < ans.size(); i++) {\n if (ans[i][0] >= h) {\n ct++;\n }\n if (ct == k) {\n idx = i+1;\n // break;\n }\n }\n ans.insert(ans.begin()+idx, sorted[j]);\n }\n return ans;\n }\n};",
"memory": "18200"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "/*********************** Chandrachur Mukherjee ***********************/\n\n#include<bits/stdc++.h>\n// #include<ext/pb_ds/assoc_container.hpp>\n// #include<ext/pb_ds/tree_policy.hpp>\n\nusing namespace std; \nusing namespace chrono;\n// using namespace __gnu_pbds;\n\n#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define MOD 1000000007\n#define MOD1 998244353\n#define INF 1e18\n#define nline \"\\n\"\n#define pb push_back\n#define ppb pop_back\n#define mp make_pair\n#define ff first\n#define ss second\n#define PI 3.141592653589793238462\n#define set_bits(x) __builtin_popcountll(x)\n#define sz(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define fr(i, n) for(int i = 0; i < n; ++i)\n#define debug(x) cout << #x << \" \"; _print(x); cout << endl;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing lld = long double;\nusing pi = pair<int, int>;\nusing pll = pair<ll, ll>;\n// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key\n\nvoid _print(ll t) {cout << t;}\nvoid _print(int t) {cout << t;}\nvoid _print(string t) {cout << t;}\nvoid _print(char t) {cout << t;}\nvoid _print(lld t) {cout << t;}\nvoid _print(double t) {cout << t;}\nvoid _print(ull t) {cout << t;}\n\ntemplate <class T, class V> void _print(pair <T, V> p);\ntemplate <class T> void _print(vector <T> v);\ntemplate <class T> void _print(set <T> v);\ntemplate <class T, class V> void _print(map <T, V> v);\ntemplate <class T> void _print(multiset <T> v);\ntemplate <class T, class V> void _print(pair <T, V> p) {cout << \"{\"; _print(p.ff); cout << \",\"; _print(p.ss); cout << \"}\";}\ntemplate <class T> void _print(vector <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\ntemplate <class T> void _print(set <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\ntemplate <class T> void _print(multiset <T> v) {cout << \"[ \"; for (T i : v) {_print(i); cout << \" \";} cout << \"]\";}\ntemplate <class T, class V> void _print(unordered_map <T, V> v) {cout << \"[ \"; for (auto i : v) {_print(i); cout << \" \";} cout << \"]\";}\ntemplate <class T, class V> void _print(map <T, V> v) {cout << \"[ \"; for (auto i : v) {_print(i); cout << \" \";} cout << \"]\";}\ntemplate <class T> void _print(stack<T> s) { while (!s.empty()) { _print(s.top()); s.pop(); if (!s.empty()) cout << \" \"; } }\ntemplate <class T> void _print(priority_queue<T> pq) { while (!pq.empty()) { _print(pq.top()); pq.pop(); if (!pq.empty()) cout << \" \"; } }\ntemplate <class T, class V, class U> void _print(priority_queue<T, V, U> pq) { while (!pq.empty()) { _print(pq.top()); pq.pop(); if (!pq.empty()) cout << \" \"; } }\ntemplate <class T> void _print(const std::vector<std::vector<T>>& arr) { std::cout << \"[\\n\"; for (const auto& row : arr) std::cout << \" \", _print(row), std::cout << \"\\n\"; std::cout << \"]\"; }\n// void _print(pbds v) {cout << \"[ \"; for (auto i : v) {_print(i); cout << \" \";} cout << \"]\";}\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n/*---------------------------------------------------------------------------------------------------------------------------*/\nll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}\nll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}\nvoid extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3\nll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b\nll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}\nbool revsort(ll a, ll b) {return a > b;}\nll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}\nvoid google(int t) {cout << \"Case \" << t << \": \";}\nvector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}\nll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\nll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}\nll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}\nll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m\nll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))\nll getRandomNumber(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);} \n/*--------------------------------------------------------------------------------------------------------------------------*/\n\n\nclass Node {\npublic:\n int sum;\n Node(int _sum = 0) : sum(_sum) {}\n};\n\nNode merge(Node a, Node b) {\n Node res;\n res.sum = a.sum + b.sum;\n return res;\n}\n\nclass SegmentTree {\n int n;\n vector<Node> t;\npublic:\n SegmentTree(int n) {\n this->n = n;\n t.resize(4 * n);\n build(1, 0, n - 1);\n }\n void build(int id, int l, int r) {\n if(l == r) {\n t[id] = Node(1);\n return;\n }\n int mid = (l + r) >> 1;\n build(id << 1, l, mid);\n build(id << 1 | 1, mid + 1, r);\n t[id] = merge(t[id << 1], t[id << 1 | 1]);\n }\n void update(int id, int l, int r, int ind, int val) {\n if(ind < l || ind > r) {\n return;\n }\n if(l == r) {\n t[id] = Node(val);\n return;\n }\n int mid = (l + r) >> 1;\n update(id << 1, l, mid, ind, val);\n update(id << 1 | 1, mid + 1, r, ind, val);\n t[id] = merge(t[id << 1], t[id << 1 | 1]);\n }\n int query(int id, int l, int r, int k) {\n if(l == r) {\n return l;\n }\n int mid = (l + r) >> 1;\n if(t[id << 1].sum >= k) {\n return query(id << 1, l, mid, k);\n } else {\n return query(id << 1 | 1, mid + 1, r, k - t[id << 1].sum);\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n = (int)people.size();\n vector<vector<int>> res(n);\n SegmentTree segtree(n);\n\n map<int, vector<int>> mp;\n for(auto &it : people) {\n int h = it[0], k = it[1];\n mp[h].push_back(k);\n }\n\n for(auto &[h, kVals] : mp) {\n vector<int> indices;\n for(auto &k : kVals) {\n int ind = segtree.query(1, 0, n - 1, k + 1);\n indices.push_back(ind);\n res[ind] = {h, k};\n }\n\n for(auto &ind : indices) {\n segtree.update(1, 0, n - 1, ind, 0);\n }\n }\n return res;\n }\n};",
"memory": "18200"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& p)\n {\n map<int,vector<int>>m;\n //number and index number\n for(int i=0;i<p.size();i++)\n {\n m[p[i][0]].push_back(p[i][1]);\n }\n int n=p.size();\n vector<vector<int>>ans(n,vector<int>(2));\n vector<int>vis(n,0);\n for(auto i:m)\n {\n vector<int>temp=i.second;\n sort(temp.begin(),temp.end());\n for(int j=0;j<temp.size();j++)\n {\n int count=0;\n for(int k=0;k<vis.size();k++)\n {\n if(!vis[k] && count==temp[j])\n {\n ans[k]={i.first,temp[j]};\n vis[k]=1;\n break;\n }\n else if(!vis[k])\n {\n count++;\n }\n else if(vis[k] && ans[k][0]>=i.first)\n {\n count++;\n }\n }\n }\n\n }\n return ans;\n }\n};",
"memory": "18300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& p)\n {\n sort(p.begin(),p.end());\n // cout<<\" p is \"<<endl;\n // for(int i=0;i<p.size();i++)\n // {\n // cout<<p[i][0]<<\" \"<<p[i][1]<<endl;\n // }\n map<int,vector<int>>m;\n //number and index number\n for(int i=0;i<p.size();i++)\n {\n m[p[i][0]].push_back(p[i][1]);\n }\n int n=p.size();\n vector<vector<int>>ans(n,vector<int>(2));\n vector<int>vis(n,0);\n for(auto i:m)\n {\n vector<int>temp=i.second;\n // sort(temp.begin(),temp.end());\n for(int j=0;j<temp.size();j++)\n {\n int count=0;\n for(int k=0;k<vis.size();k++)\n {\n if(!vis[k] && count==temp[j])\n {\n ans[k]={i.first,temp[j]};\n vis[k]=1;\n break;\n }\n else if(!vis[k])\n {\n count++;\n }\n else if(vis[k] && ans[k][0]>=i.first)\n {\n count++;\n }\n }\n }\n\n }\n return ans;\n }\n};",
"memory": "18300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> mp;\n for(auto it : people){\n mp[it[1]].insert(it[0]);\n }\n vector<vector<int>> res ;\n for(int i = 0 ;i < people.size();i++){\n if(mp.find(i)==mp.end())continue;\n for(auto it : mp[i]){\n int ans = 0 ; \n int loc = res.size();\n for(int j = 0 ; j < res.size();j++){\n if(ans>i){loc-=1;break;}\n if(res[j][0]>=it){\n ans++;\n }\n loc=j+1;\n }\n if(ans>i&&loc==res.size())loc--;\n res.insert(res.begin()+loc , {it,i});\n \n cout<<endl;\n }\n }\n return res;\n }\n \n};",
"memory": "18400"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> mp;\n for(auto it : people){\n mp[it[1]].insert(it[0]);\n }\n vector<vector<int>> res ;\n for(int i = 0 ;i < people.size();i++){\n if(mp.find(i)==mp.end())continue;\n for(auto it : mp[i]){\n int ans = 0 ; \n int loc = res.size();\n for(int j = 0 ; j < res.size();j++){\n if(ans>i){loc-=1;break;}\n if(res[j][0]>=it){\n ans++;\n }\n loc=j+1;\n }\n if(ans>i&&loc==res.size())loc--;\n res.insert(res.begin()+loc , {it,i});\n \n }\n }\n return res;\n }\n \n};",
"memory": "18500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\n\nprivate: \n static bool compare(vector<int>& A, vector<int>& B){\n \n if(A[0] == B[0]){\n return A[1] < B[1];\n }\n return A[0] > B[0];\n \n }\n\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n \n // 7,0 4,4, 7,1 5,0, 6,1 5,2\n // 5,0 7,0 5,2 6,1, 4,4, 7,1\n \n // 5 7 4 6 7 2 4\n // 5,0 7,0 ,4,0 \n // 6,0 7,0 2,3, 4,3\n \n // We would always have a 0 \n // 7,0, 4,0, 6,0 ,7,0 2 4\n \n // O(N^2)\n //\n \n // Those who have 0 will be in the list\n \n vector<vector<int>> peopleList;\n for(auto info: people){\n peopleList.push_back({info[0], info[1]});\n }\n \n sort(peopleList.begin(), peopleList.end(), compare);\n \n list<vector<int>> order;\n \n for(auto peoples: peopleList){\n auto itr = order.begin();\n advance(itr, peoples[1]);\n order.insert(itr, peoples);\n }\n \n vector<vector<int>> answer(order.begin(), order.end());\n return answer;\n \n }\n \n};",
"memory": "18900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n map<int, set<int>> mp;\n for (auto a : people) {\n mp[a[0]].insert(a[1]);\n }\n vector<vector<int>> res;\n for (auto it = mp.rbegin(); it != mp.rend(); ++it) {\n for (auto p : it->second) {\n res.insert(res.begin() + p, {it->first, p});\n }\n }\n return res;\n }\n};",
"memory": "19100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "static auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();\n\n#pragma GCC optimize(\"tree-vectorize\")\n#pragma GCC target(\"tune=native\")\n#pragma GCC optimize(\"Ofast\")\n\n\nclass Solution {\npublic:\n std::vector<std::vector<int>> reconstructQueue(std::vector<std::vector<int>>& people) {\n std::sort(people.begin(), people.end(),\n [](const auto& v1, const auto& v2) {\n if (v1[0] == v2[0]) {\n return v1[1] < v2[1]; \n }\n return v1[0] > v2[0];\n }\n );\n\n std::deque<std::vector<int>> result;\n for (std::size_t i = 0; i < people.size(); ++i) {\n result.insert(result.begin() + people[i][1], people[i]);\n }\n\n return {result.begin(), result.end()};\n }\n};\n\n/*\n\n\n[[1,0]]\n[[1,1],[2,0]]\n[[1,2],[2,1],[7,0]]\n\n\n[[1,2],[2,1],[7,0],[6,0]]\n[[1,2],[2,1],[7,0],[6,0],[4,2]]\n[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4],[11,0],[9,0]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4],[11,0],[9,1]]\n[[3,3],[1,5],[2,4],[4,2],[5,1],[6,0]]\n[[9,0],[7,0],[1,9],[3,0],[2,7],[5,3],[6,0],[3,4],[6,2],[5,2]]\n\n\n*/",
"memory": "19800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "static auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();\n\n#pragma GCC optimize(\"tree-vectorize\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2\")\n\n\nclass Solution {\npublic:\n std::vector<std::vector<int>> reconstructQueue(std::vector<std::vector<int>>& people) {\n std::sort(people.begin(), people.end(),\n [](const auto& v1, const auto& v2) {\n if (v1[0] == v2[0]) {\n return v1[1] < v2[1]; \n }\n return v1[0] > v2[0];\n }\n );\n\n std::deque<std::vector<int>> result;\n for (std::size_t i = 0; i < people.size(); ++i) {\n result.insert(result.begin() + people[i][1], people[i]);\n }\n\n return {result.begin(), result.end()};\n }\n};\n\n/*\n\n\n[[1,0]]\n[[1,1],[2,0]]\n[[1,2],[2,1],[7,0]]\n\n\n[[1,2],[2,1],[7,0],[6,0]]\n[[1,2],[2,1],[7,0],[6,0],[4,2]]\n[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4],[11,0],[9,0]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4],[11,0],[9,1]]\n[[3,3],[1,5],[2,4],[4,2],[5,1],[6,0]]\n[[9,0],[7,0],[1,9],[3,0],[2,7],[5,3],[6,0],[3,4],[6,2],[5,2]]\n\n\n*/",
"memory": "19800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "static auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();\n\n#pragma GCC optimize(\"tree-vectorize\")\n#pragma GCC target(\"tune=native\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2\")\n\n\nclass Solution {\npublic:\n std::vector<std::vector<int>> reconstructQueue(std::vector<std::vector<int>>& people) {\n std::sort(people.begin(), people.end(),\n [](const auto& v1, const auto& v2) {\n if (v1[0] == v2[0]) {\n return v1[1] < v2[1]; \n }\n return v1[0] > v2[0];\n }\n );\n\n std::deque<std::vector<int>> result;\n for (std::size_t i = 0; i < people.size(); ++i) {\n result.insert(result.begin() + people[i][1], people[i]);\n }\n\n return {result.begin(), result.end()};\n }\n};\n\n/*\n\n\n[[1,0]]\n[[1,1],[2,0]]\n[[1,2],[2,1],[7,0]]\n\n\n[[1,2],[2,1],[7,0],[6,0]]\n[[1,2],[2,1],[7,0],[6,0],[4,2]]\n[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4],[11,0],[9,0]]\n[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4],[11,0],[9,1]]\n[[3,3],[1,5],[2,4],[4,2],[5,1],[6,0]]\n[[9,0],[7,0],[1,9],[3,0],[2,7],[5,3],[6,0],[3,4],[6,2],[5,2]]\n\n\n*/",
"memory": "19900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "#include<list>\nclass Solution {\npublic:\n \n static bool customComparator(vector<int>& v1, vector<int>& v2) {\n bool verdict = false;\n if (v1[1] < v2[1]) {\n verdict = true;\n }\n else if (v2[1] < v1[1]) {\n verdict = false;\n }\n else if (v1[0] < v2[0]) {\n verdict = true;\n }\n else if (v2[0] < v1[0]) {\n verdict = false;\n }\n // cout<<verdict<<\"\\n\";\n return verdict;\n }\n \n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n deque<vector<int> > transformedPeople;\n vector<vector<int> > reconstructedQueue;\n for(int i = 0;i < people.size(); i++) {\n vector<int> temp;\n temp.push_back(people[i][0]);\n temp.push_back(people[i][1]);\n temp.push_back(i);\n transformedPeople.push_back(temp);\n }\n sort(transformedPeople.begin(), transformedPeople.end(), customComparator);\n \n while(reconstructedQueue.size() < people.size()) {\n \n int index = transformedPeople[0][2];\n reconstructedQueue.push_back(people[index]);\n transformedPeople.pop_front();\n for(int i = 0;i < transformedPeople.size();i++) {\n \n if (transformedPeople[i][0] <= people[index][0]) {\n transformedPeople[i][1]--;\n }\n }\n sort(transformedPeople.begin(), transformedPeople.end(), customComparator);\n }\n return reconstructedQueue;\n }\n};\n\n",
"memory": "21400"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool cmp(const vector<int> &a, vector<int>b)\n {\n if (a[0] == b[0]) return a[1] < b[1];\n return a[0] > b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n\n int size = people.size();\n vector<vector<int>> ans ;\n //\n sort (people.begin(), people.end(), cmp);\n\n for (int i = 0; i < size; i++) {\n int pos = people[i][1];\n ans.insert(ans.begin() + pos, people[i]);\n }\n\n return ans;\n }\n};",
"memory": "21700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool cmp(vector<int> &a, vector<int>b)\n {\n if (a[0] == b[0]) return a[1] < b[1];\n return a[0] > b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n\n int size = people.size();\n vector<vector<int>> ans ;\n //\n sort (people.begin(), people.end(), cmp);\n\n for (int i = 0; i < size; i++) {\n int pos = people[i][1];\n ans.insert(ans.begin() + pos, people[i]);\n }\n\n return ans;\n }\n};",
"memory": "21800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n std::vector<std::vector<int>> reconstructQueue(std::vector<std::vector<int>>& people) \n {\n std::sort( people.begin(), people.end(), []( std::vector<int>first, std::vector<int>& second ){\n return first[0] == second[0] ? first[1] < second[1] : first[0] > second[0];\n }); \n vector<vector<int>> res;\n int n = people.size(); \n \n for(int i = 0; i < n; i++)\n {\n // place the curr people in its correct position\n \n int pos = people[i][1];\n \n res.insert(res.begin() + pos, people[i]);\n }\n \n return res;\n }\n};\n",
"memory": "22000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n std::vector<std::vector<int>> reconstructQueue(std::vector<std::vector<int>>& people) \n {\n std::sort( people.begin(), people.end(), []( std::vector<int>first, std::vector<int>& second ){\n return first[0] == second[0] ? first[1] < second[1] : first[0] > second[0];\n }); \n\n std::vector<std::vector<int>> res;\n int n = people.size(); \n \n for(int i = 0; i < n; i++)\n {\n int pos = people[i][1];\n res.insert(res.begin() + pos, people[i]);\n }\n \n return res;\n }\n};\n",
"memory": "22000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool cmp(const vector<int> &a,const vector<int>b)\n {\n if (a[0] == b[0]) return a[1] < b[1];\n return a[0] > b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n\n int size = people.size();\n vector<vector<int>> ans ;\n //\n sort (people.begin(), people.end(), cmp);\n\n for (int i = 0; i < size; i++) {\n int pos = people[i][1];\n ans.insert(ans.begin() + pos, people[i]);\n }\n\n return ans;\n }\n};",
"memory": "22100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [](const vector<int> & a, const vector<int>b){\n if(a[0]!=b[0]){\n return a[0]>b[0];\n }\n return a[1]<b[1];\n });\n vector<vector<int>> queue;\n for(auto person: people){\n queue.insert(queue.begin()+person[1], person);\n }\n return queue;\n }\n};",
"memory": "22500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n=people.size();\n new_sort(people);\n vector<int> v(n);\n vector<int> r;\n for(int i=0;i<v.size();i++){\n if(people[i][1]==0){\n v[i]=1;\n r.push_back(people[i][0]);\n }\n }\n sort(r.begin(),r.end());\n vector<vector<int>> res;\n for(int i=0;i<r.size();i++){\n vector<int>e(2);\n e[0]=r[i];\n res.push_back(e);\n }\n for(int i=0;i<n;i++){\n if(v[i]==0){\n // cout<<people[i][0]<<\" \"<<people[i][1]<<\"\\n\";\n joiner(res,people[i]);\n }\n }\n return res;\n\n\n }\n void joiner(vector<vector<int>> &res,vector<int> d){\n if(res.size()<=d[1]){\n res.push_back(d);\n return;\n }\n int count=0;\n vector<int> swap=d;\n vector<int> c=d;\n for(int i=0;i<res.size();i++){\n if(res[i][0]>=d[0]){\n count++;\n }\n if(count==d[1]){\n for(int j=i+1;j<res.size();j++){\n c=res[j];\n res[j]=swap;\n swap=c;\n }\n res.push_back(c);\n break;\n }\n \n }\n \n }\n void new_sort(vector<vector<int>> &p){\n map<int,vector<int>> m;\n for(int i=0;i<p.size();i++){\n if(m.find(p[i][0])==m.end()){\n vector<int> r;\n r.push_back(p[i][1]);\n m[p[i][0]]=r;\n }\n else{\n m[p[i][0]].push_back(p[i][1]);\n }\n }\n p=vector<vector<int>>();\n for(auto it=m.rbegin();it!=m.rend();it++){\n vector<int>q=it->second;\n sort(q.begin(),q.end());\n for(int j=0;j<q.size();j++){\n vector<int>s(2,it->first);\n s[1]=q[j];\n p.push_back(s);\n }\n }\n }\n};",
"memory": "23100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n constexpr static int maxH=1048576;\n constexpr static int maxN=2001;\n int tree[2*maxH];\n int used[maxN];\n void update(int pos,int val){\n pos+=maxH;\n tree[pos]+=val;\n pos/=2;\n while(pos){\n tree[pos]=tree[2*pos]+tree[2*pos+1];\n pos/=2;\n }\n }\n int querry(int a,int b){\n int total=0;\n a+=maxH-1;\n b+=maxH+1;\n while(a/2!=b/2){\n if(a%2==0){total+=tree[a+1];}\n if(b%2==1){total+=tree[b-1];}\n a/=2;\n b/=2;\n }\n return total;\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int x=people.size();\n vector<vector<int>> res;\n\n while(x){\n int min_val=INT_MAX;\n int min_idx=-1;\n for(int i=0;i<people.size();i++){\n if(used[i])continue;\n int higher=querry(people[i][0],maxH-1);\n if(higher==people[i][1]&&people[i][0]<min_val){\n min_val=people[i][0];\n min_idx=i;\n }\n }\n used[min_idx]=true;\n update(min_val,1);\n res.push_back(people[min_idx]);\n x--;\n }\n return res;\n }\n};",
"memory": "24700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n #define n 1000001\n int segTree[2*n];\n void update(int i){\n i+=n;\n segTree[i]++;\n for(i;i>1;i>>=1)segTree[i>>1]=segTree[i]+segTree[i^1];\n }\n int query(int l,long r){\n int sum=0;\n for(l+=n,r+=n;l<r;l>>=1,r>>=1){\n if(l&1)sum+=segTree[l++];\n if(r&1)sum+=segTree[--r];\n }\n return sum;\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n vector<vector<int>>ans;\n sort(people.begin(),people.end());\n map<int,int>mp;\n for(int i=0;i<people.size();i++){\n for(int j=0;j<people.size();j++){\n if(mp[j]>0)continue;\n if(query(people[j][0],1000001)==people[j][1]){\n mp[j]++;\n update(people[j][0]);\n ans.push_back(people[j]);\n break;\n }\n }\n }\n return ans;\n }\n};",
"memory": "25600"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n = people.size();\n auto comp = [](vector<int> x, vector<int> y) {\n if (x[0] == y[0]) {\n return x[1] > y[1];\n }\n return x[0] > y[0];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> minHeap(comp);\n for (const auto& data : people) {\n minHeap.push(data);\n }\n\n vector<vector<int>> answer(n, vector<int>{-1,-1});\n while(!minHeap.empty()) {\n int value = minHeap.top()[0];\n int count = minHeap.top()[1];\n for (int i = 0; i < n; i++) {\n if (answer[i][0] != -1 && answer[i][0] < value) {\n continue;\n }\n if (count == 0) {\n answer[i] = minHeap.top();\n break;\n }\n if (answer[i][0] == -1 || answer[i][0] >= value) {\n count--;\n }\n }\n minHeap.pop();\n }\n\n return answer;\n }\n};\n\n// [5,0] [7,0] [6,1] [7,1] [5,2] [4,4]\n// [4,4] [5,0] [5,2] [6,1] [7,0] [7,1]\n\n// [5,0] [7,0] [5,2] [6,1] [4,4] [7,1]\n\n\n// XXXXXX\n// 012041\n",
"memory": "28100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool static mycomp(vector<int>v1, vector<int>v2){\n if(v1[0] != v2[0]){\n return v1[0]<v2[0];\n }\n else{\n return v1[1] < v2[1];\n }\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(),mycomp);\n int n = people.size();\n vector<vector<int>>ans(n,vector<int>(2,-1));\n for(int i=0; i<n; i++){\n int height = people[i][0];\n int count = people[i][1];\n int j=0;\n if(count == 0){\n while(ans[j][0] != -1){\n j++;\n }\n }\n else{\n while(count>0){\n if(ans[j][0]>=height || ans[j][0] == -1){\n count--;\n }\n j++;\n }\n }\n while(ans[j][0] != -1){\n j++;\n }\n ans[j] = people[i];\n }\n return ans;\n }\n};",
"memory": "28300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool compare(vector<int> a, vector<int> b)\n {\n if(a[0] == b[0])\n return b[1] < a[1];\n \n return a[0] < b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) \n {\n sort(people.begin(),people.end(),compare);\n int n = people.size();\n \n vector<vector<int>> ans(n, vector<int>(2,-1));\n\n for(int i=0;i<n;i++)\n {\n int count = people[i][1];\n for(int j=0;j<n;j++)\n {\n if(ans[j][0] == -1 && count == 0)\n {\n ans[j][0] = people[i][0];\n ans[j][1] = people[i][1];\n break;\n }\n else\n if(ans[j][0] == -1)\n count--;\n }\n }\n\n return ans;\n }\n};",
"memory": "28500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool static mycomp(vector<int>v1, vector<int>v2){\n if(v1[0] != v2[0]){\n return v1[0]<v2[0];\n }\n else{\n return v1[1] < v2[1];\n }\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(),mycomp);\n int n = people.size();\n vector<vector<int>>ans(n,vector<int>(2,-1));\n for(int i=0; i<n; i++){\n int height = people[i][0];\n int count = people[i][1];\n int j=0;\n if(count == 0){\n while(ans[j][0] != -1){\n j++;\n }\n }\n else{\n while(count>0){\n if(ans[j][0]>=height || ans[j][0] == -1){\n count--;\n }\n j++;\n }\n }\n while(ans[j][0] != -1){\n j++;\n }\n ans[j] = people[i];\n }\n return ans;\n }\n};",
"memory": "28500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int>tree;\n void build(int node,int start,int end) {\n if(start==end) {\n tree[node]=1; \n return;\n }\n int mid=(start+end)/2;\n build(2*node,start,mid);\n build(2*node+1,mid+1,end);\n tree[node]=tree[2*node]+tree[2*node+1];\n }\n\n int query(int node,int start,int end, int k) {\n if(start==end) {\n return start;\n }\n int mid=(start+end)/2;\n int left=tree[2*node];\n int right=tree[2*node+1];\n if(k>left){\n return query(2*node+1,mid+1,end, k-left);\n }else{\n return query(2*node,start,mid,k);\n }\n }\n\n void update(int node,int start,int end,int idx) {\n if(start==end) {\n tree[node]=0; \n return;\n }\n int mid=(start+end)/2;\n if(idx<=mid){\n update(2*node,start,mid,idx);\n }else{\n update(2*node+1,mid+1,end,idx);\n }\n tree[node]=tree[2*node]+tree[2*node+1];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(),[](vector<int>a,vector<int>b){\n if(a[0]!=b[0]) return a[0]<b[0];\n return a[1]>b[1];\n });\n int n=people.size();\n tree.clear();\n tree.resize(4*n+10);\n vector<vector<int>> res(people.size(), vector<int>());\n build(1,0,n-1);\n for(int i = 0; i < people.size(); i++){\n int j=query(1,0,n-1,people[i][1]+1);\n res[j]=people[i];\n update(1,0,n-1,j);\n }\n return res;\n }\n};",
"memory": "28600"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int>tree;\n void build(int node,int start,int end) {\n if(start==end) {\n tree[node]=1; \n return;\n }\n int mid=(start+end)/2;\n build(2*node,start,mid);\n build(2*node+1,mid+1,end);\n tree[node]=tree[2*node]+tree[2*node+1];\n }\n\n int query(int node,int start,int end, int k) {\n if(start==end) {\n return start;\n }\n int mid=(start+end)/2;\n int left=tree[2*node];\n int right=tree[2*node+1];\n if(k>left){\n return query(2*node+1,mid+1,end, k-left);\n }else{\n return query(2*node,start,mid,k);\n }\n }\n\n void update(int node,int start,int end,int idx) {\n if(start==end) {\n tree[node]=0; \n return;\n }\n int mid=(start+end)/2;\n if(idx<=mid){\n update(2*node,start,mid,idx);\n }else{\n update(2*node+1,mid+1,end,idx);\n }\n tree[node]=tree[2*node]+tree[2*node+1];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(),[](vector<int>a,vector<int>b){\n if(a[0]!=b[0]) return a[0]<b[0];\n return a[1]>b[1];\n });\n int n=people.size();\n tree.clear();\n tree.resize(4*n+10);\n vector<vector<int>> res(people.size(), vector<int>());\n build(1,0,n-1);\n for(int i = 0; i < people.size(); i++){\n int j=query(1,0,n-1,people[i][1]+1);\n res[j]=people[i];\n update(1,0,n-1,j);\n }\n return res;\n }\n};",
"memory": "28700"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool static cmp(vector<int> a, vector<int> b){\n if(a[0] == b[0]){\n return a[1] < b[1];\n }\n return a[0] > b[0]; \n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n int n = people.size();\n sort(people.begin(), people.end(), cmp);\n vector<vector<int>> ans(n, vector<int>());\n for(int i = 0; i < n; i++){\n int idx = people[i][1];\n\n if(ans[idx].size() == 0){\n ans[idx] = people[i];\n }else{\n int j = n - 2;\n while(j >= 0 && j >= idx){\n swap(ans[j], ans[j+1]);\n j--;\n }\n ans[idx] = people[i];\n }\n }\n return ans;\n }\n};",
"memory": "28800"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n #define ll long long\n static const bool cmp(vector<int> a,vector<int>b){\n if(a[0]==b[0]){\n return a[1]>b[1];\n }\n return a[0]<b[0];\n }\n\n vector<ll> seg;\n void build(int i,int tl,int tr){\n if(tl==tr){\n seg[i] = 1;\n return;\n }\n\n int mid = (tl+tr)/2;\n build(2*i,tl,mid);\n build(2*i+1,mid+1,tr);\n\n seg[i] = seg[2*i] + seg[2*i+1];\n }\n\n void update(int i,int tl,int tr,int pos){\n if(tl==tr){\n seg[i] = 0;\n return;\n }\n\n int mid = (tl+tr)/2;\n if(pos<=mid){\n update(2*i,tl,mid,pos);\n }\n else{\n update(2*i+1,mid+1,tr,pos);\n }\n\n seg[i] = seg[2*i] + seg[2*i+1];\n }\n\n int query(int i,int tl,int tr,int k){\n if(tl==tr){\n return tl;\n }\n\n int mid = (tl+tr)/2;\n int left = seg[2*i];\n if(k<=left){\n return query(2*i,tl,mid,k);\n }\n else{\n return query(2*i+1,mid+1,tr,k-left);\n }\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(), cmp);\n int n = people.size();\n seg.resize(4*n);\n\n vector<vector<int>> ans(n,vector<int>());\n build(1,0,n-1);\n\n for(int i=0;i<n;i++){\n int j = query(1,0,n-1,people[i][1]+1);\n ans[j] = people[i];\n update(1,0,n-1,j);\n }\n\n return ans;\n }\n};",
"memory": "28900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int>tree;\n void build(int node,int start,int end) {\n if(start==end) {\n tree[node]=1; \n return;\n }\n int mid=(start+end)/2;\n build(2*node,start,mid);\n build(2*node+1,mid+1,end);\n tree[node]=tree[2*node]+tree[2*node+1];\n }\n\n int query(int node,int start,int end, int k) {\n if(start==end) {\n return start;\n }\n int mid=(start+end)/2;\n int left=tree[2*node];\n int right=tree[2*node+1];\n if(k>left){\n return query(2*node+1,mid+1,end, k-left);\n }else{\n return query(2*node,start,mid,k);\n }\n }\n\n void update(int node,int start,int end,int idx) {\n if(start==end) {\n tree[node]=0; \n return;\n }\n int mid=(start+end)/2;\n if(idx<=mid){\n update(2*node,start,mid,idx);\n }else{\n update(2*node+1,mid+1,end,idx);\n }\n tree[node]=tree[2*node]+tree[2*node+1];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(),[](vector<int>a,vector<int>b){\n if(a[0]!=b[0]) return a[0]<b[0];\n return a[1]>b[1];\n });\n int n=people.size();\n tree.clear();\n tree.resize(4*n+10);\n vector<vector<int>> res(people.size(), vector<int>());\n build(1,0,n-1);\n for(int i = 0; i < people.size(); i++){\n int j=query(1,0,n-1,people[i][1]+1);\n res[j]=people[i];\n update(1,0,n-1,j);\n }\n return res;\n }\n};",
"memory": "28900"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n class compare{\n\n public:\n\n bool operator()(vector<int> a , vector<int> b){\n\n\n if(a[1] == b[1]){\n return a[0]>b[0];\n }\n\n return a[1]>b[1];\n }\n };\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n \n priority_queue< vector<int> , vector<vector<int>> , compare > q;\n\n int n = people.size();\n\n //map<int> visit;\n\n for(int i=0 ; i<n ; i++){\n q.push(people[i]);\n }\n\n vector<vector<int>> ans;\n // vector<int> a;\n\n \n while(!q.empty()){\n\n vector<int> tp = q.top();\n // ans.push_back(tp);\n\n if(tp[1]==0){\n ans.push_back(tp);\n }\n\n else{\n int a = tp[0];\n int b = tp[1];\n int n = ans.size();\n int i=0;\n\n while(i<n && b>0){\n if(ans[i][0]>=a){\n b--;\n }\n i++;\n }\n\n if(b==0){\n while(i<n&& a>ans[i][0]){\n i++;\n }\n }\n\n ans.insert(ans.begin()+i , tp);\n\n }\n\n \n q.pop();\n }\n\n return ans;\n\n\n }\n};",
"memory": "29000"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nstatic bool cmp(vector<int> a , vector<int> b){\n if(a[0]==b[0])\n return a[1]<b[1];\n return a[0]>b[0];\n}\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(),cmp);\n vector<vector<int>> ans;\n for(int i=0;i<people.size();i++){\n ans.insert(ans.begin()+people[i][1],people[i]);\n }\n return ans;\n }\n};",
"memory": "29100"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool cmp(vector<int> a, vector<int> b){\n if(a[0]==b[0]) return a[1]<b[1];\n return a[0]>b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& ppl) {\n sort(ppl.begin(),ppl.end(),cmp);\n int n = ppl.size();\n vector<vector<int>> ans;\n for(int i=0;i<n;i++){\n ans.insert(ans.begin()+ppl[i][1], ppl[i]);\n }\n return ans;\n }\n};",
"memory": "29200"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [&](vector<int> a, vector<int> b){\n if(a[0] < b[0]) return true;\n else if(a[0] > b[0]) return false;\n else{\n return (a[1] < b[1]);\n }\n });\n vector<vector<int>> answer(people.size(), vector<int>({-1}));\n for(const auto x : people){\n int i = 0;\n int a = 0;\n while(i < x[1] && (i+a < answer.size()-1)){\n if(answer[i+a][0] != -1 && answer[i+a][0] != x[0]) a++;\n else i++;\n }\n while(((i+a) < (answer.size()-1)) && (answer[i+a][0] != -1)) a++;\n answer[i+a] = x;\n }\n return answer;\n }\n};",
"memory": "29300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [&](vector<int> a, vector<int> b){\n if(a[0] > b[0]) return true;\n else if(a[0] < b[0]) return false;\n else{\n return (a[1] < b[1]);\n }\n });\n vector<vector<int>> answer;\n for(const auto& x : people){\n answer.insert(answer.begin()+x[1], x);\n }\n return answer;\n }\n};",
"memory": "29300"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n struct myCompare{\n bool operator()(std::vector<int> left, std::vector<int> right){\n if(left[0] == right[0]){return left[1]< right[1];}\n return left[0] > right[0];\n }\n };\n\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n std::vector<std::vector<int>> result;\n std::sort(people.begin(), people.end(),myCompare());\n\n for(int i=0; i<people.size(); i++){\n result.insert(result.begin() + people[i][1], people[i]);\n }\n return result;\n }\n};",
"memory": "29400"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\n static bool cmp (vector<int>a, vector<int>b){\n if (a[0]==b[0]) return a[1]<b[1];\n return a[0]>b[0];\n }\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(),cmp);\n vector<vector<int>> result;\n for (auto each : people){\n result.insert(result.begin()+each[1] , each);\n }\n return result;\n }\n};",
"memory": "29500"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(),people.end(),[&](const vector<int>a,const vector<int>b)\n {\n if(a[1]==b[1])\n return a[0]<b[0];\n\n return a[1]<b[1];\n });\n\n vector<vector<int> >ans;\n\n for(int i=0;i<people.size();i++)\n {\n if(people[i][1]==0)\n ans.push_back({people[i][0],people[i][1]});\n else\n {\n int a = people[i][0] , b = people[i][1];\n int cnt = 0;\n\n bool flg=false;\n\n for(int j=0;j<ans.size();j++)\n {\n if(ans[j][0]>=a)\n cnt++;\n\n if(cnt>b)\n {\n vector<int>temp={a,b};\n ans.insert(ans.begin()+j,temp);\n flg=true;\n break;\n }\n }\n\n if(!flg)\n ans.push_back({a,b});\n }\n }\n\n return ans;\n }\n};\n\n/*\n\n5,0 7,0 6,1 7,1 5,2 4,4\n\n5,0 7,0 5,2 6,1 4,4 7,1\n\n*/",
"memory": "29600"
} |
406 | <p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p>
<p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
<strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
<strong>Explanation:</strong>
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
<strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= people.length <= 2000</code></li>
<li><code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code></li>
<li><code>0 <= k<sub>i</sub> < people.length</code></li>
<li>It is guaranteed that the queue can be reconstructed.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool myComp(vector<int> a, vector<int> b) {\n if(a[0] == b[0]){\n return a[1] < b[1];\n }\n return a[0] > b[0];\n }\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin() , people.end(), myComp);\n for(auto i: people) {\n cout << i[0] << \" \" << i[1] << endl;\n }\n vector<vector<int>> ans;\n for(int i = 0; i < people.size(); i++) {\n int pos = people[i][1];\n ans.insert(ans.begin() + pos, people[i]);\n }\n return ans;\n }\n};",
"memory": "29700"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPerfectSquare(long long num) {\n long long x;\n if (num==1){return true;}\n for(long long i=1; i<=num/2; i++){\n if((x=i*i)==num){return true;}\n if(x>num){return false;}\n }\nreturn false;}\n};",
"memory": "7000"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPerfectSquare(int n) {\n int i = 0, j = 0, f = 0;\n int s = 0, e = n/2;\n long long int m = (e-s)/2 + s;\n\n if(n == 1) return true;\n\n while(s <= e){\n long long int b = m * m;\n if(b == n) return true;\n else if(b > n) e = m - 1;\n else s = m + 1;\n\n m = (e-s)/2 + s;\n }\n\n return false;\n }\n};",
"memory": "7100"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPerfectSquare(int num) {\n if (num < 0) return false;\n int low = 0, high = num;\n\n while (low <= high) {\n long mid = (high + low) / 2;\n long Square = mid * mid;\n if (Square == num) return true;\n else if (Square < num) \n low = mid + 1;\n else \n high = mid - 1;\n\n }\n\n return false;\n }\n};",
"memory": "7100"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:double __sqrt(int n)\n{\n double l=0,r=n,ans=n;\n int i=0;\n while(i<100)\n {\n double m=(l+r)/2;\n if(m*m<=n)\n {\n l=m;\n ans=m;\n }\n else r=m;\n i++;\n }\n return ans;\n}\n bool isPerfectSquare(int num) {\n int ans=__sqrt(num);\n return ans*ans==num;\n \n }\n};",
"memory": "7200"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPerfectSquare(int num) {\n if(num==1)\n {\n return true;\n }\n for(long long i=2;i<=num/2;i++)\n {\n if(i*i==num)\n {\n return true;\n }\n }\n return false;\n }\n};",
"memory": "7200"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPerfectSquare(int num) {\n int x =sqrt(num);\n return x*x==num;\n }\n};",
"memory": "7300"
} |
367 | <p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 16
<strong>Output:</strong> true
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPerfectSquare(int num) {\n if (num < 1) \n return false;\n\n int low = 1;\n int high = num;\n\n while(high >= low){\n int mid = low + (high-low)/2;\n long long square = static_cast<long long>(mid)*mid;\n if(square == num)\n return true;\n if(square > num)\n high = mid - 1;\n else\n low = mid + 1;\n }\n return false;\n }\n};",
"memory": "7300"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n if(edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1]) return edges[0][0];\n else return edges[0][1]; \n }\n};\n\nint init = [] {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tofstream out(\"user.out\");\n\tcout.rdbuf(out.rdbuf());\n\tfor (string line; getline(cin, line); cout << '\\n') {\n\t\tistringstream ss(line);\n\t\tchar ch;\n\t\tint a, b, c, d;\n\t\tss >> ch >> ch >> a >> ch >> b >> ch >> ch >> ch >> c >> ch >> d;\n\t\tif (a == c || a == d) cout << a;\n\t\telse cout << b;\n\t}\n\texit(0);\n\treturn 0;\n}();",
"memory": "14464"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int ans;\n if(edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1]){\n ans = edges[0][0];\n } \n\n if(edges[0][1] == edges[1][0] || edges[0][1] == edges[1][1]){\n ans = edges[0][1];\n }\n return ans;\n }\n};\n\nint init = [] {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tofstream out(\"user.out\");\n\tcout.rdbuf(out.rdbuf());\n\tfor (string line; getline(cin, line); cout << '\\n') {\n\t\tistringstream ss(line);\n\t\tchar ch;\n\t\tint a, b, c, d;\n\t\tss >> ch >> ch >> a >> ch >> b >> ch >> ch >> ch >> c >> ch >> d;\n\t\tif (a == c || a == d) cout << a;\n\t\telse cout << b;\n\t}\n\texit(0);\n\treturn 0;\n}();",
"memory": "14464"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n cout.tie(0);\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n int a = edges[0][0], b = edges[0][1], c = edges[1][0], d = edges[1][1];\n if(a == c || a == d) return a;\n else return b; \n \n }\n};",
"memory": "15593"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n if(edges[0][0]==edges[1][0]||edges[0][0]==edges[1][1])\n return edges[0][0];\n else\n return edges[0][1];\n }\n};",
"memory": "16721"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int a = ((edges[0][0]==edges[1][0]) || (edges[0][0]==edges[1][1]))? edges[0][0]:edges[0][1];\n return a;\n }\n};",
"memory": "17850"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n\n if(edges[0][0] == edges[1][0]|| edges[0][0] == edges[1][1])\n return edges[0][0];\n else\n return edges[0][1];\n \n }\n};",
"memory": "18979"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n if(edges[0][0]==edges[1][0] || edges[0][0]== edges[1][1]){\n return edges[0][0];\n }\n else{\n return edges[0][1];\n }\n \n }\n};",
"memory": "18979"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n if(edges[0][0]==edges[1][0] || edges[0][0]==edges[1][1])\n return edges[0][0];\n return edges[0][1];\n }\n};",
"memory": "20108"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n return edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] ? edges[0][0] : edges[0][1];\n }\n};",
"memory": "20108"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int a = edges[0][1];\n int b = edges[0][0];\n if(edges[1][0] ==a || edges[1][1] ==a)\n return a;\n return b;\n\n }\n};",
"memory": "21236"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edge) {\n // vector<int>first=edge[0];//{a,b}\n // vector<int>second=edge[1];//{c,a} \n // if(first[0]==second[0]|| first[0]==second[1]){\n // return first[0];\n // }\n // return first[1];\n int a=edge[0][0];\n int b=edge[0][1];\n int c=edge[1][0];\n int d=edge[1][1];\n return (c==a || c==b)?c:d;\n }\n};",
"memory": "21236"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n\n vector<bool> visited(n+1,false);\n\n for(int i=0;i<n-1;i++){\n int u = edges[i][0], v = edges[i][1];\n \n if(visited[u]) return u;\n if(visited[v]) return v;\n\n visited[u] = true;\n visited[v] = true;\n }\n\n return -1;\n }\n};",
"memory": "22365"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n bitset<100000> visited=0;\n for(auto& e: edges){\n int v=e[0], w=e[1];\n if (visited[v]) return v;\n if (visited[w]) return w;\n visited[v]=visited[w]=1;\n }\n return -1;\n }\n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "30266"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size();\n vector<int> degree(n +2 , 0); \n\n for (auto &edge : edges) {\n degree[edge[0]]++;\n degree[edge[1]]++;\n\n \n if (degree[edge[0]] == n) return edge[0];\n if (degree[edge[1]] == n) return edge[1];\n }\n return 0; } \n};\n",
"memory": "30266"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n // Initialize a vector to count degrees of each node\n // Use maximum possible size based on edge counts\n vector<int> count(edges.size() + 2, 0);\n \n // Count the degree of each node\n for (const auto& edge : edges) {\n // Increment the count for both nodes in the edge\n count[edge[0]]++;\n count[edge[1]]++;\n }\n \n // The center node will have a degree equal to the number of edges\n int n = edges.size() + 1; // Total number of nodes in the star graph\n for (int i = 1; i <= n; ++i) {\n if (count[i] == n - 1) {\n return i;\n }\n }\n \n return -1; // In case no center is found, which should not happen for a valid star graph\n }\n};\n",
"memory": "31395"
} |
1,916 | <p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" />
<pre>
<strong>Input:</strong> edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i,</sub> v<sub>i</sub> <= n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The given <code>edges</code> represent a valid star graph.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges)\n {\n vector<int> cnt(edges.size()+2, 0);\n for (int i = 0; i < edges.size()-1; i++)\n {\n if (edges[i][0] == edges[i+1][0] || edges[i][0] == edges[i+1][1]) return edges[i][0];\n if (edges[i][1] == edges[i+1][0] || edges[i][1] == edges[i+1][1]) return edges[i][1];\n }\n return -1;\n }\n};",
"memory": "31395"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.