2024/11/30 灵茶题单 单调栈
This commit is contained in:
parent
a1caf8fe22
commit
61bf855b75
|
@ -0,0 +1,25 @@
|
||||||
|
package com.cool.ling_cha_mount.stack;
|
||||||
|
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Num1124 {
|
||||||
|
public int longestWPI(int[] hours) {
|
||||||
|
int res = 0;
|
||||||
|
int[] arr = new int[hours.length + 1];
|
||||||
|
Deque<Integer> stack = new LinkedList<>();
|
||||||
|
stack.push(0);
|
||||||
|
for (int i = 1; i <= hours.length; i++) {
|
||||||
|
arr[i] = arr[i - 1] + (hours[i - 1] > 8 ? 1 : -1);
|
||||||
|
if (arr[i] < arr[stack.peek()]) {
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = hours.length; i > 0; i--) {
|
||||||
|
while (!stack.isEmpty() && arr[i] > arr[stack.peek()]) {
|
||||||
|
res = Math.max(res, i - stack.pop());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue