单调栈-Maximum Width Ramp
2020-01-23 19:39:26
问题描述:

问题求解:
public int maxWidthRamp(int[] A) {
Stack<Integer> stack = new Stack<>();
int res = 0;
int n = A.length;
for (int i = 0; i < n; i++) {
if (stack.isEmpty() || A[stack.peek()] > A[i]) {
stack.add(i);
}
}
for (int i = n - 1; i > res; i--) {
while (!stack.isEmpty() && A[stack.peek()] <= A[i]) {
res = Math.max(res, i - stack.pop());
}
}
return res;
}

浙公网安备 33010602011771号