[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

 

1. ๋ฌธ์ œ : Link

๊ฑด๋„๋•Œ๋งˆ๋‹ค 1๋งŒํผ ์ง€๋‚˜๊ฐˆ๋•Œ๋งˆ๋‹ค 1์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๊ณ 

๋‹ค๋ฆฌ๊ฐ€ ์ง€ํƒฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ๋งŒํผ์˜ ์ฐจ๋งŒ ์ง€๋‚˜๋‹ค๋‹ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ์„๋•Œ

์ตœ๋‹จ์‹œ๊ฐ„์œผ๋กœ ๊ฑด๋„ ๋•Œ์˜ ์‹œ๊ฐ„

 

2. ํ’€์ด

queue๋ฅผ ์ด์šฉํ•ด์„œ  ๋‹ค๋ฆฌ์œ„์—์žˆ๋Š” ๋ชจ๋“  ์ฐจ์˜ ๋ฌด๊ฒŒ์™€ ์ƒˆ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฌด๊ฒŒ๋ฅผ ํ•ฉํ•˜์—ฌ ๊ฐ๋‹น๊ฐ€๋Šฅํ•œ ๋ฌด๊ฒŒ์™€ ๋น„๊ตํ–ˆ๋‹ค.

 

3. ์ฝ”๋“œ

def solution(bridge_length, weight, truck_weights):
    bridge = [truck_weights.pop(0)]
    bridge_count = [1]
    time = 1
    while 1:
        time += 1
        if not truck_weights:
            time += bridge_length-1
            break
        else:
            if bridge_count[0] == bridge_length:
                bridge_count.pop(0)
                bridge.pop(0)
            if sum(bridge) + truck_weights[0] <= weight:
                bridge.append(truck_weights.pop(0))
                bridge_count.append(0)
        for i in range(len(bridge_count)):
            bridge_count[i] += 1
    return time

์ข€ ๋” ๋‚˜์€ ์ฝ”๋“œ

def solution(bridge_length, weight, truck_weights):
    
    answer = 0
    bridge = [0 for _ in range(bridge_length)]
    
    while bridge:
        
        answer += 1
        bridge.pop(0)
        
        if truck_weights:
            if sum(bridge) + truck_weights[0] <= weight:            
                t = truck_weights.pop(0)
                bridge.append(t)
            else:
                bridge.append(0)
                 
         
    return answer
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ