Skip to content

Commit d0e3563

Browse files
committed
Merge branch 'master' of github.com:everthis/leetcode-js
2 parents efee188 + 7570adb commit d0e3563

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

.gitignore

Whitespace-only changes.

406-queue-reconstruction-by-height.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} people
3+
* @return {number[][]}
4+
*/
5+
const reconstructQueue = function(people) {
6+
if (!people) return []
7+
const peopledct = {}
8+
let height = []
9+
const res = []
10+
people.forEach((el, idx) => {
11+
if (peopledct.hasOwnProperty(el[0])) {
12+
peopledct[el[0]].push([el[1], idx])
13+
} else {
14+
peopledct[el[0]] = [[el[1], idx]]
15+
height.push(el[0])
16+
}
17+
})
18+
height = height.sort((a, b) => b - a)
19+
20+
for (let i = 0; i < height.length; i++) {
21+
peopledct[height[i]] = peopledct[height[i]].sort((a, b) => a[0] - b[0])
22+
for (el of peopledct[height[i]]) {
23+
res.splice(el[0],0, people[el[1]])
24+
}
25+
}
26+
return res
27+
};
28+
29+
console.log(reconstructQueue([[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]))

496-next-greater-element-I.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} findNums
3+
* @param {number[]} nums
4+
* @return {number[]}
5+
*/
6+
const nextGreaterElement = function(findNums, nums) {
7+
const map = {}
8+
const stack = []
9+
for(let num of nums) {
10+
while(stack.length && stack[stack.length - 1] < num) {
11+
let tmp = stack.pop()
12+
map[tmp] = num
13+
}
14+
stack.push(num)
15+
}
16+
for (let i = 0; i < findNums.length; i++) {
17+
findNums[i] = map[findNums[i]] == null ? -1 : map[findNums[i]]
18+
}
19+
20+
return findNums
21+
};
22+
23+
console.log(nextGreaterElement([4,1,2], [1,3,4,2]))
24+
console.log(nextGreaterElement([2,4], [1,2,3,4]))

0 commit comments

Comments
 (0)