Skip to content

Commit

Permalink
징검다리 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeVicTech committed Jan 2, 2023
1 parent 46dcfdc commit 557aeeb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 이진탐색/징검다리.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#https://school.programmers.co.kr/learn/courses/30/lessons/43236

def solution(distance, rocks, n):
start = 0
end = distance

rocks.sort()#바위를 위치순으로 정렬
rocks.append(distance)#도착지점을 추가함

while start <= end:
mid = (start + end)//2
print(mid)
standard = 0
num = 0
for rock in rocks:
between = rock - standard
if mid <= between:
standard = rock
else:
num += 1

if num > n:
end = mid - 1
else:
answer = mid#다른 사람들의 풀이를 보면 여기서 미드가 꼭 거리의 최솟값이라는 보장이 없다고 생각해서 돌거리 최소값을 따로 갱신해 주는 경우가 있음
start = mid + 1

return answer

solution(25,[2, 14, 11, 21, 17],2)

0 comments on commit 557aeeb

Please sign in to comment.