Skip to content

Commit 5948c51

Browse files
authored
Create PG_징검다리건너기.java
1 parent 4ecdfb5 commit 5948c51

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 프로그래머스 징검다리 건너기
3+
* - 매개변수 탐색
4+
* - 0이 k-1개 연속으로 나와야 건널 수 있음
5+
*/
6+
7+
class Solution {
8+
public int solution(int[] stones, int k) {
9+
int answer = 0;
10+
11+
int left = Integer.MAX_VALUE;
12+
int right = Integer.MIN_VALUE;
13+
14+
for (int stone : stones) {
15+
left = Math.min(left, stone);
16+
right = Math.max(right, stone);
17+
}
18+
19+
while (left <= right) {
20+
int mid = left + (right - left) / 2;
21+
22+
int zeroCount = 0;
23+
24+
for (int stone : stones) {
25+
if (stone - mid <= 0) zeroCount++;
26+
else zeroCount = 0;
27+
28+
if (zeroCount >= k) break;
29+
}
30+
31+
if (zeroCount < k) {
32+
answer = mid;
33+
left = mid + 1;
34+
} else right = mid - 1;
35+
}
36+
37+
return left;
38+
}
39+
}

0 commit comments

Comments
 (0)