-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2560.java
More file actions
31 lines (30 loc) · 893 Bytes
/
Copy pathSolution2560.java
File metadata and controls
31 lines (30 loc) · 893 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
class Solution {
public int minCapability(int[] nums, int k) {
int lower = Arrays.stream(nums).min().getAsInt();
int upper = Arrays.stream(nums).max().getAsInt();
while (lower <= upper) {
int middle = (lower + upper) / 2;
int count = getCount(nums, middle);
if (count >= k) {
upper = middle - 1;
} else {
lower = middle + 1;
}
}
return lower;
}
public int getCount(int[] nums, int max) {
// 在不超过 max 情况下所能窃取的最大房屋数量
int count = 0;
boolean visited = false;
for (int x : nums) {
if (x <= max && !visited) {
count++;
visited = true;
} else {
visited = false;
}
}
return count;
}
}