-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (27 loc) · 1.22 KB
/
Copy pathsolution.java
File metadata and controls
34 lines (27 loc) · 1.22 KB
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 binarySearchable(int[] arr) {
// I keep the answer in a field-like array so the nested helper can update it.
int[] count = new int[1];
// I start the recursive walk with the full index range and no value limits.
dfs(arr, 0, arr.length - 1, Long.MIN_VALUE / 4, Long.MAX_VALUE / 4, count);
return count[0];
}
private void dfs(int[] arr, int l, int r, long low, long high, int[] count) {
// An empty segment means the binary-search path ends here.
if (l > r)
return;
// The middle position is the same one binary search would inspect first.
int mid = l + (r - l) / 2;
long val = arr[mid];
// If the value stays inside the allowed range, this index is searchable.
if (val > low && val < high) {
count[0]++;
}
// The left side must stay below the current value, so I tighten the upper
// bound.
dfs(arr, l, mid - 1, low, Math.min(high, val), count);
// The right side must stay above the current value, so I tighten the lower
// bound.
dfs(arr, mid + 1, r, Math.max(low, val), high, count);
}
};