Algorithms/Programmers

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μž…κ΅­μ‹¬μ‚¬ (이뢄탐색)

탱저 2021. 11. 5. 13:37

https://programmers.co.kr/learn/courses/30/lessons/43238

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - μž…κ΅­μ‹¬μ‚¬

nλͺ…이 μž…κ΅­μ‹¬μ‚¬λ₯Ό μœ„ν•΄ 쀄을 μ„œμ„œ 기닀리고 μžˆμŠ΅λ‹ˆλ‹€. 각 μž…κ΅­μ‹¬μ‚¬λŒ€μ— μžˆλŠ” μ‹¬μ‚¬κ΄€λ§ˆλ‹€ μ‹¬μ‚¬ν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„μ€ λ‹€λ¦…λ‹ˆλ‹€. μ²˜μŒμ— λͺ¨λ“  μ‹¬μ‚¬λŒ€λŠ” λΉ„μ–΄μžˆμŠ΅λ‹ˆλ‹€. ν•œ μ‹¬μ‚¬λŒ€μ—μ„œλŠ” λ™μ‹œμ— ν•œ

programmers.co.kr

def solution(n, times):
    answer = 0
    left = 1
    right = max(times) * n # κ°€μž₯ λΉ„νš¨μœ¨μ μΈ 심사 μ‹œκ°„
    while left <= right:
        mid = (left+ right) // 2
        count = 0
        for i in times:
            count += mid // i
            # λͺ¨λ“  심사관 μ•ˆκ±°μΉ˜κ³  midλΆ„ λ™μ•ˆ nλͺ… μ΄μƒμ˜ 심사λ₯Ό ν•  수 μžˆλ‹€λ©΄ break
            if count >= n:
                break
        if count >= n:
            right = mid - 1
        elif count < n:
            left = mid + 1
    return left

print(solution(6, [7, 10]))
728x90