[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Lv2] ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ / Python

๐Ÿ“š Problem Solving/Programmers

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

ํŠธ๋Ÿญ ์—ฌ๋Ÿฌ ๋Œ€๊ฐ€ ๊ฐ•์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์ผ์ฐจ์„  ๋‹ค๋ฆฌ๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์œผ๋กœ ๊ฑด๋„ˆ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š”์ง€ ์•Œ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฆฌ์—๋Š” ํŠธ๋Ÿญ์ด ์ตœ๋Œ€ bridge_length๋Œ€ ์˜ฌ๋ผ๊ฐˆ

programmers.co.kr

import sys

input = sys.stdin.readline

bridge_length, weight = map(int, input().split())
truck_weights = list(map(int, input().split()))


def solution(bridge_length, weight, truck_weights):
    bridge = [0] * bridge_length
    time = 0
    while bridge:
        time += 1
        bridge.pop(0)
        if truck_weights:
            if sum(bridge) + truck_weights[0] <= weight:
                bridge.append(truck_weights.pop(0))
            else:
                bridge.append(0)
    return time


print(solution(bridge_length, weight, truck_weights))

 

ํ•ด์„ค

๋‹ค๋ฆฌ ๊ธธ์ด๋งŒํผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. 1์ดˆ๊ฐ€ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์•ž ๊ฐ’์„ ๋นผ์ค€๋‹ค. ์ด ํ›„, ๋ฆฌ์ŠคํŠธ์™€ truck_weights์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์˜  ํ•ฉ์ด weight๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ, truck_weight์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค. ๋งŒ์•ฝ ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š”๋‹ค๋ฉด 0์„ ๋„ฃ์–ด์ค€๋‹ค.