2024/11/27 灵茶题单 单调栈
This commit is contained in:
parent
4b9d1922eb
commit
44bfda06ef
|
@ -0,0 +1,29 @@
|
||||||
|
package com.cool.ling_cha_mount.stack;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Num962 {
|
||||||
|
public int maxWidthRamp(int[] nums) {
|
||||||
|
Deque<Integer> stack = new LinkedList<>();
|
||||||
|
int res=0;
|
||||||
|
stack.push(0);
|
||||||
|
for (int i = 1; i < nums.length; i++) {
|
||||||
|
if (nums[i] < nums[stack.peek()]) {
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i=nums.length-1;i>=0;i--){
|
||||||
|
while (!stack.isEmpty()&&nums[stack.peek()]<=nums[i]){
|
||||||
|
res=Math.max(res,i-stack.pop());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
maxWidthRamp(new int[]{6,0,8,2,1,5});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue