Maximum Width Ramp LeetCode solution in Java

1
class Solution {
2
public int maxWidthRamp(int[] nums) {
3
Stack<Integer> s = new Stack<>();
4
int res = 0;
5
for (int i = 0; i < nums.length; i++) {
6
if (!s.isEmpty() && nums[s.peek()] <= nums[i]) {
7
res = Math.max(res, i - s.peek());
8
continue;
9
}
10
s.push(i);
11
}
12
int i = nums.length - 1;
13
while (!s.isEmpty() && i >= 0) {
14
if (nums[s.peek()] <= nums[i]) {
15
res = Math.max(res, i - s.peek());
16
s.pop();
17
} else {
18
i--;
19
}
20
}
21
return res;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0