목차

징검다리

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호43236
문제명징검다리
레벨Level 4
분류

파라메트릭 서치

시간복잡도O(mlogn)
인풋사이즈n<=1,000,000,000, m<=50,000
사용한 언어Python
해결날짜2020/12/31
태그

고득점 Kit - 이분탐색

풀이

코드

"""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)