-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 119.java
More file actions
34 lines (29 loc) · 888 Bytes
/
Day 119.java
File metadata and controls
34 lines (29 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public int maxPeople(int[] arr) {
int n = arr.length;
int[] left = new int[n];
int[] right = new int[n];
Stack<Integer> st = new Stack<>();
for (int i = 0; i < n; i++) {
while (!st.isEmpty() && arr[st.peek()] < arr[i]) {
st.pop();
}
left[i] = st.isEmpty() ? -1 : st.peek();
st.push(i);
}
st.clear();
for (int i = n - 1; i >= 0; i--) {
while (!st.isEmpty() && arr[st.peek()] < arr[i]) {
st.pop();
}
right[i] = st.isEmpty() ? n : st.peek();
st.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++) {
int visible = (i - left[i] - 1) + (right[i] - i - 1) + 1;
ans = Math.max(ans, visible);
}
return ans;
}
}