[알고리즘] 이분탐색

이현영·2023년 2월 15일

알고리즘

목록 보기
2/3

1️⃣ left right 설정하기
2️⃣ while True 반복, mid 설정

while True:
    mid = (left+right)//2

3️⃣ mid 값을 이용하여 주어진 조건에 만족하는지 체크하기 위한 값 구하기

    count = 0
    for line in lines:
        count += line//mid

4️⃣ left > right 면 반복 끝내고 값 출력

    if left > right:
        print(mid)
        break

5️⃣ 주어진 조건 만족에 따라 leftright 값을 조절한다

    if count < n:
        right = mid - 1

    else:
        left = mid + 1

예시 문제
BOJ 1654

profile
티스토리로 이전했습니다 | https://hamo0.tistory.com/

0개의 댓글