Skip to content

Commit b0e820c

Browse files
committed
[level 4] Title: 징검다리, Time: 120.49 ms, Memory: 19.6 MB -BaekjoonHub
1 parent 310b086 commit b0e820c

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [level 4] 징검다리 - 43236
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/43236)
4+
5+
### 성능 요약
6+
7+
메모리: 19.6 MB, 시간: 120.49 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 이분탐색
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 10월 28일 00:08:26
20+
21+
### 문제 설명
22+
23+
<p>출발지점부터 distance만큼 떨어진 곳에 도착지점이 있습니다. 그리고 그사이에는 바위들이 놓여있습니다. 바위 중 몇 개를 제거하려고 합니다.<br>
24+
예를 들어, 도착지점이 25만큼 떨어져 있고, 바위가 [2, 14, 11, 21, 17] 지점에 놓여있을 때 바위 2개를 제거하면 출발지점, 도착지점, 바위 간의 거리가 아래와 같습니다.</p>
25+
<table class="table">
26+
<thead><tr>
27+
<th>제거한 바위의 위치</th>
28+
<th>각 바위 사이의 거리</th>
29+
<th>거리의 최솟값</th>
30+
</tr>
31+
</thead>
32+
<tbody><tr>
33+
<td>[21, 17]</td>
34+
<td>[2, 9, 3, 11]</td>
35+
<td>2</td>
36+
</tr>
37+
<tr>
38+
<td>[2, 21]</td>
39+
<td>[11, 3, 3, 8]</td>
40+
<td>3</td>
41+
</tr>
42+
<tr>
43+
<td>[2, 11]</td>
44+
<td>[14, 3, 4, 4]</td>
45+
<td>3</td>
46+
</tr>
47+
<tr>
48+
<td>[11, 21]</td>
49+
<td>[2, 12, 3, 8]</td>
50+
<td>2</td>
51+
</tr>
52+
<tr>
53+
<td>[2, 14]</td>
54+
<td>[11, 6, 4, 4]</td>
55+
<td>4</td>
56+
</tr>
57+
</tbody>
58+
</table>
59+
<p>위에서 구한 거리의 최솟값 중에 가장 큰 값은 4입니다.</p>
60+
61+
<p>출발지점부터 도착지점까지의 거리 distance, 바위들이 있는 위치를 담은 배열 rocks, 제거할 바위의 수 n이 매개변수로 주어질 때, 바위를 n개 제거한 뒤 각 지점 사이의 거리의 최솟값 중에 가장 큰 값을 return 하도록 solution 함수를 작성해주세요.</p>
62+
63+
<h5>제한사항</h5>
64+
65+
<ul>
66+
<li>도착지점까지의 거리 distance는 1 이상 1,000,000,000 이하입니다.</li>
67+
<li>바위는 1개 이상 50,000개 이하가 있습니다.</li>
68+
<li>n 은 1 이상 <code>바위의 개수</code> 이하입니다.</li>
69+
</ul>
70+
71+
<h5>입출력 예</h5>
72+
<table class="table">
73+
<thead><tr>
74+
<th>distance</th>
75+
<th>rocks</th>
76+
<th>n</th>
77+
<th>return</th>
78+
</tr>
79+
</thead>
80+
<tbody><tr>
81+
<td>25</td>
82+
<td>[2, 14, 11, 21, 17]</td>
83+
<td>2</td>
84+
<td>4</td>
85+
</tr>
86+
</tbody>
87+
</table>
88+
<h5>입출력 예 설명</h5>
89+
90+
<p>문제에 나온 예와 같습니다.</p>
91+
92+
<p><a href="http://contest.usaco.org/DEC06.htm" target="_blank" rel="noopener">출처</a></p>
93+
94+
<p>※ 공지 - 2020년 2월 17일 테스트케이스가 추가되었습니다.<br>
95+
※ 공지 - 2023년 5월 15일 테스트케이스가 추가되었습니다. 기존에 제출한 코드가 통과하지 못할 수도 있습니다.</p>
96+
97+
98+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Foundation
2+
3+
func solution(_ distance:Int, _ rocks:[Int], _ n:Int) -> Int {
4+
let sortedRocks = rocks.sorted()
5+
6+
let allPoints = [0] + sortedRocks + [distance]
7+
8+
var start = 1
9+
var end = distance
10+
var answer = 0
11+
12+
while start <= end {
13+
let mid = (start + end) / 2
14+
15+
var removedCount = 0
16+
var lastPos = 0
17+
18+
for i in 1..<allPoints.count {
19+
let currentPos = allPoints[i]
20+
21+
let gap = currentPos - lastPos
22+
23+
if gap < mid {
24+
removedCount += 1
25+
} else {
26+
lastPos = currentPos
27+
}
28+
}
29+
30+
if removedCount <= n {
31+
answer = mid
32+
start = mid + 1
33+
} else {
34+
end = mid - 1
35+
}
36+
}
37+
38+
return answer
39+
}

0 commit comments

Comments
 (0)