ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ง•๊ฒ€๋‹ค๋ฆฌ

์ถœ๋ฐœ์ง€์ ๋ถ€ํ„ฐ distance๋งŒํผ ๋–จ์–ด์ง„ ๊ณณ์— ๋„์ฐฉ์ง€์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ์‚ฌ์ด์—๋Š” ๋ฐ”์œ„๋“ค์ด ๋†“์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”์œ„ ์ค‘ ๋ช‡ ๊ฐœ๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋„์ฐฉ์ง€์ ์ด 25๋งŒํผ ๋–จ์–ด์ ธ ์žˆ๊ณ , ๋ฐ”์œ„๊ฐ€

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

 ์ถœ๋ฐœ์ง€์ ๋ถ€ํ„ฐ ๊ฑฐ๋ฆฌ๋งŒํผ ๋–จ์–ด์ง„ ๊ณณ์— ๋„์ฐฉ์ง€์ ์ด ์žˆ๋‹ค. ์ด๋•Œ ๊ทธ ์‚ฌ์ด์— ์žˆ๋Š” ๋ฐ”์œ„๋“ค ์ค‘ ๋ช‡ ๊ฐœ๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํŠน์ • ๋ฐ”์œ„๋ฅผ `N`๊ฐœ๋งŒํผ ์ œ๊ฑฐํ•  ๋•Œ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 ์ด ๋ฌธ์ œ๋Š” ๋ฌธ์ œ์˜ ๋ถ„๋ฅ˜์™€ ๊ฐ™์ด `์ด๋ถ„ ํƒ์ƒ‰`์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. `์ด๋ถ„ ํƒ์ƒ‰`์„ ํ†ตํ•ด ์ตœ์†Œ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ, ๋น ์ง„ ๋Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ ํ›„ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ์œผ๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ

from math import inf

def solution(distance, rocks, n):
    answer = 0
    rocks.sort()
    start, end = 0, distance
    
    while start <= end:
        mid = (start + end) // 2
        prev, rock_cnt = 0, 0
        cur_min = inf
        
        for rock in rocks:
            if rock - prev < mid:
                rock_cnt += 1
            else:
                cur_min = min(cur_min, rock - prev)
                prev = rock
        
        if rock_cnt > n:
            end = mid - 1
        else:
            answer = cur_min
            start = mid + 1
                
    return answer
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€