Skip to content

Commit 296904d

Browse files
authored
Update 503-next-greater-element-II.js
1 parent 5160bb7 commit 296904d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

503-next-greater-element-II.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const nextGreaterElements = function(nums) {
6+
const arr = []
7+
const n = nums.length
8+
const res = Array(n).fill(-1)
9+
nums.push(...nums)
10+
const stk = []
11+
for(let i = 0; i < 2 * n; i++) {
12+
const e = nums[i]
13+
while(stk.length && nums[stk.at(-1)] < e) {
14+
const idx = stk.pop()
15+
res[idx] = e
16+
}
17+
if(i < n) stk.push(i)
18+
}
19+
20+
return res
21+
};
22+
23+
// another
24+
25+
126
/**
227
* @param {number[]} nums
328
* @return {number[]}

0 commit comments

Comments
 (0)