๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Level 3 - ์ž…๊ตญ ์‹ฌ์‚ฌ

by HandHand 2021. 3. 1.

๋ฌธ์ œ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์ž…๊ตญ ์‹ฌ์‚ฌ

ํ’€์ด ๊ณผ์ •

์ด ๋ฌธ์ œ๋Š” ์ž…๋ ฅ๊ฐ’์ด ๋งค์šฐ ํฌ๊ฒŒ ์ฃผ์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ํƒ€์ž„ ์•„์›ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

ํšจ์œจ์„ฑ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์ฒ˜์Œ ๋– ์˜ฌ๋ฆฐ ํ•ด๋ฒ•์€ times ๋ฐฐ์—ด๊ณผ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ jobs ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ๋’ค,
ํ•ด๋‹น ๋ฐฐ์—ด์—๋Š” ํ˜„์žฌ ์†๋‹˜์˜ ์—…๋ฌด๊ฐ€ ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜๊ณ  ๋‹ค์Œ๋ฒˆ ์†๋‹˜์˜ ์ž…๊ตญ ์‹ฌ์‚ฌ๋Œ€๋ฅผ ๊ณ ๋ฅผ๋•Œ๋Š”
๊ฐ ์‹ฌ์‚ฌ๋Œ€์˜ ์‹œ๊ฐ„์„ jobs ๋ฐฐ์—ด์— ๊ฐ๊ฐ ๋”ํ•ด๋ณธ ๋‹ค์Œ ๊ฐ€์žฅ ์งง์€ ์‹ฌ์‚ฌ๋Œ€๋กœ ๋ฐฐ์น˜์‹œํ‚ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•์€ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ ๋ณด๋‹ค ํšจ์šธ์ ์ธ ํƒ์ƒ‰์„ ์œ„ํ•ด ์ด์ง„ ํƒ์ƒ‰ ์„ ํ™œ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ตœ์•…์˜ ๊ฒฝ์šฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„์€ ์–ผ๋งˆ์ผ๊นŒ์š”? ๊ทธ๊ฑด ๋ฐ”๋กœ ๋ชจ๋“  ์†๋‹˜์ด ๊ฐ€์žฅ ๋Š๋ฆฐ ์‹ฌ์‚ฌ๋Œ€์—์„œ ์‹ฌ์‚ฌ๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ hi ๋ฅผ ์ด ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์ค€ ๋‹ค์Œ, mid ์— ํ•ด๋‹นํ•˜๋Š” ์‹œ๊ฐ„์— ์†๋‹˜์„ ์ „๋ถ€ ๊ฐ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ๋” ์ž‘์€ ์ชฝ์œผ๋กœ,
๋งŒ์•ฝ ๊ฐ๋‹น์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ๋” ํฐ ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ์„œ ์ตœ์ ์˜ ์‹œ๊ฐ„์„ ์ฐพ์•„๋ƒˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์— ์†๋‹˜์„ ์ „๋ถ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์—์„œ๋Š” ๊ฐ ์‹ฌ์‚ฌ๊ด€๋งˆ๋‹ค
์ฃผ์–ด์ง„ ์‹œ๊ฐ„ ๋‚ด์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์†๋‹˜์˜ ์ˆ˜๋ฅผ ํ•ฉํ•ด์„œ
๋ชฉํ‘œ ์†๋‹˜์ˆ˜์— ๋„๋‹ฌํ–ˆ๋‹ค๋ฉด ๋ฐ”๋กœ True ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰ ์‹œ๊ฐ„์„ ์ค„์˜€์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ


def can_work(times, due, goal):
    for time in times:
        goal -= (due // time)
        if goal <= 0:
            return True
    return False


def solution(n, times):
    answer = 0
    lo = 1
    hi = n * max(times)

    while lo < hi:
        mid = (lo + hi) // 2
        if can_work(times, mid, n):
            answer = mid
            hi = mid
        else:
            lo = mid + 1

    return answer


n = 6
times = [7, 10]
print(solution(n, times))  # ๋‹ต์€ 28
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€