Skip to content

Commit 5160bb7

Browse files
authored
Update 496-next-greater-element-I.js
1 parent a2be85f commit 5160bb7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

496-next-greater-element-I.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[]}
5+
*/
6+
const nextGreaterElement = function(nums1, nums2) {
7+
const map = new Map()
8+
const stk = []
9+
10+
for(let i = 0, n = nums2.length; i < n; i++) {
11+
const e = nums2[i]
12+
while(stk.length && stk.at(-1) < e) {
13+
const tmp = stk.pop()
14+
map.set(tmp, e)
15+
}
16+
stk.push(e)
17+
}
18+
19+
const res = []
20+
for(const e of nums1) {
21+
if(map.has(e)) {
22+
res.push(map.get(e))
23+
} else {
24+
res.push(-1)
25+
}
26+
}
27+
28+
return res
29+
};
30+
31+
// another
32+
133
/**
234
* @param {number[]} findNums
335
* @param {number[]} nums

0 commit comments

Comments
 (0)