Skip to content

Commit 8822329

Browse files
committedFeb 25, 2025
Add solution #406
1 parent 5b7d053 commit 8822329

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@
324324
403|[Frog Jump](./0403-frog-jump.js)|Hard|
325325
404|[Sum of Left Leaves](./0404-sum-of-left-leaves.js)|Easy|
326326
405|[Convert a Number to Hexadecimal](./0405-convert-a-number-to-hexadecimal.js)|Easy|
327+
406|[Queue Reconstruction by Height](./0406-queue-reconstruction-by-height.js)|Medium|
327328
407|[Trapping Rain Water II](./0407-trapping-rain-water-ii.js)|Hard|
328329
412|[Fizz Buzz](./0412-fizz-buzz.js)|Easy|
329330
414|[Third Maximum Number](./0414-third-maximum-number.js)|Easy|
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 406. Queue Reconstruction by Height
3+
* https://leetcode.com/problems/queue-reconstruction-by-height/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array of people, people, which are the attributes of some people in a queue
7+
* (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height
8+
* hi with exactly ki other people in front who have a height greater than or equal to hi.
9+
*
10+
* Reconstruct and return the queue that is represented by the input array people. The returned
11+
* queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of
12+
* the jth person in the queue (queue[0] is the person at the front of the queue).
13+
*/
14+
15+
/**
16+
* @param {number[][]} people
17+
* @return {number[][]}
18+
*/
19+
var reconstructQueue = function(people) {
20+
return people
21+
.sort(([h1, k1], [h2, k2]) => h2 - h1 || k1 - k2)
22+
.reduce((queue, person) => queue.splice(person[1], 0, person) && queue, []);
23+
};

0 commit comments

Comments
 (0)