-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMS_64062.java
More file actions
46 lines (35 loc) · 990 Bytes
/
Copy pathPMS_64062.java
File metadata and controls
46 lines (35 loc) · 990 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
40
41
42
43
44
45
46
package programmers;
public class PMS_64062 {
public static void main(String[] args) {
System.out.println(solution(new int[]{2, 4, 5, 3, 2, 1, 4, 2, 5, 1}, 3));
}
public static int solution(int[] stones, int k) {
int answer = 0;
int min = 1;
int max = 200000000;
while (min <= max) {
int mid = (min + max) / 2;
if (canAcrossRiver(stones, k, mid)) {
min = mid + 1;
answer = Math.max(answer, mid);
} else {
max = mid - 1;
}
}
return answer;
}
public static boolean canAcrossRiver(int[] stones, int k, int friends) {
int skip = 0;
for (int stone : stones) {
if (stone - friends < 0) {
skip++;
} else {
skip = 0;
}
if (skip == k) {
return false;
}
}
return true;
}
}