Skip to content

Commit 38e9c4f

Browse files
authored
Update Solution.ts
1 parent b4bc492 commit 38e9c4f

File tree

1 file changed

+10
-7
lines changed
  • solution/0500-0599/0503.Next Greater Element II

1 file changed

+10
-7
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
function nextGreaterElements(nums: number[]): number[] {
2-
const stack: number[] = [], len = nums.length;
3-
const res: number[] = new Array(len).fill(-1);
2+
let stack: number[] = [],
3+
len = nums.length;
4+
let res: number[] = new Array(len).fill(-1);
45
for (let i = 0; i < 2 * len - 1; i++) {
5-
const j = i % len;
6-
while (stack.length !== 0 && nums[stack[stack.length - 1]] < nums[j]) {
7-
res[stack[stack.length - 1]] = nums[j];
6+
while (
7+
stack.length !== 0 &&
8+
nums[stack[stack.length - 1]] < nums[i % len]
9+
) {
10+
res[stack[stack.length - 1]] = nums[i % len];
811
stack.pop();
912
}
10-
stack.push(j);
13+
stack.push(i % len);
1114
}
1215
return res;
13-
};
16+
}

0 commit comments

Comments
 (0)