-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_64062.java
More file actions
38 lines (31 loc) · 992 Bytes
/
JY_64062.java
File metadata and controls
38 lines (31 loc) · 992 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
class JY_64062 {
public int solution(int[] stones, int k) {
int answer = 0;
int s = 0;
int e = Integer.MAX_VALUE;
// stones[i] <= 200,000,000 이므로 max로 초기화
while(s <= e) {
int mid = (s+e) / 2;
if(canGo(mid, stones, k)) {
answer = mid;
s = mid + 1;
} else {
e = mid - 1;
}
}
return answer;
}
public static boolean canGo(int mid, int[] stones, int k) {
int cnt = 0; // 건널 수 없는 돌의 개수
for(int i=0; i<stones.length; i++) {
if(stones[i] - mid < 0) {
cnt++;
} else {
cnt = 0;
}
// 건널 수 없는 돌의 개수가 연속으로 k개 이상되면 불가능
if(cnt >= k) return false;
}
return true;
}
}