-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
39 lines (29 loc) · 987 Bytes
/
Copy pathsolution.java
File metadata and controls
39 lines (29 loc) · 987 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
35
36
37
38
39
class Solution {
public int bitonic(int[] arr) {
int n = arr.length;
// Stores length of non-decreasing subarray ending at each index
int[] inc = new int[n];
for (int i = 0; i < n; i++)
inc[i] = 1;
// Build increasing lengths
for (int i = 1; i < n; i++) {
if (arr[i] >= arr[i - 1])
inc[i] = inc[i - 1] + 1;
}
// Stores length of non-increasing subarray starting at each index
int[] dec = new int[n];
for (int i = 0; i < n; i++)
dec[i] = 1;
// Build decreasing lengths
for (int i = n - 2; i >= 0; i--) {
if (arr[i] >= arr[i + 1])
dec[i] = dec[i + 1] + 1;
}
int ans = 1;
// Calculate the best bitonic length
for (int i = 0; i < n; i++) {
ans = Math.max(ans, inc[i] + dec[i] - 1);
}
return ans;
}
}