ps | |
---|---|
링크 | programmers.co.kr/… |
출처 | 프로그래머스 |
문제 번호 | 43236 |
문제명 | 징검다리 |
레벨 | Level 4 |
분류 |
파라메트릭 서치 |
시간복잡도 | O(mlogn) |
인풋사이즈 | n<=1,000,000,000, m<=50,000 |
사용한 언어 | Python |
해결날짜 | 2020/12/31 |
태그 |
"""Solution code for "Programmers 43236. 징검다리".
- Problem link: https://programmers.co.kr/learn/courses/30/lessons/43236
- Solution link: http://www.teferi.net/ps/problems/programmers/43236
"""
from teflib import binsearch
def solution(distance, rocks, n):
def is_valid(min_dist):
prev_rock = 0
removed_rock_count = 0
for rock in rocks:
if rock - prev_rock >= min_dist:
prev_rock = rock
else:
removed_rock_count += 1
if distance - prev_rock < min_dist:
removed_rock_count += 1
return removed_rock_count <= n
rocks.sort()
beg, end = 0, distance // (len(rocks) - n + 1) + 1
return binsearch.maximum_valid_integer(beg, end, is_valid)