[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] ์Šคํƒ/ํ : ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

2021. 7. 20. 19:17

๋ฌธ์ œ ์„ค๋ช…

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

์˜ˆ๋ฅผ ๋“ค์–ด, ํŠธ๋Ÿญ 2๋Œ€๊ฐ€ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๊ณ  ๋ฌด๊ฒŒ๋ฅผ 10kg๊นŒ์ง€ ๊ฒฌ๋””๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด๊ฒŒ๊ฐ€ [7, 4, 5, 6]kg์ธ ํŠธ๋Ÿญ์ด ์ˆœ์„œ๋Œ€๋กœ ์ตœ๋‹จ ์‹œ๊ฐ„ ์•ˆ์— ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฑด๋„ˆ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฝ๊ณผ ์‹œ๊ฐ„๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚œ ํŠธ๋Ÿญ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ํŠธ๋Ÿญ๋Œ€๊ธฐ ํŠธ๋Ÿญ

0 [] [] [7,4,5,6]
1~2 [] [7] [4,5,6]
3 [7] [4] [5,6]
4 [7] [4,5] [6]
5 [7,4] [5] [6]
6~7 [7,4,5] [6] []
8 [7,4,5,6] [] []

๋”ฐ๋ผ์„œ, ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋ ค๋ฉด ์ตœ์†Œ 8์ดˆ๊ฐ€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํŠธ๋Ÿญ ์ˆ˜ bridge_length, ๋‹ค๋ฆฌ๊ฐ€ ๊ฒฌ๋”œ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ weight, ํŠธ๋Ÿญ ๋ณ„ ๋ฌด๊ฒŒ truck_weights๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • bridge_length๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • weight๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • truck_weights์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋Š” 1 ์ด์ƒ weight ์ดํ•˜์ž…๋‹ˆ๋‹ค.

 

์ •๋‹ต

from collections import deque

def solution(bridge_length, weight, truck_weights):
    
    if sum(truck_weights) <= weight:
        return bridge_length + len(truck_weights)
    
    truck_weights = deque(truck_weights)    # ํŠธ๋Ÿญ ๋ณ„ ๋ฌด๊ฒŒ ๋ฆฌ์ŠคํŠธ deque๋กœ ๋ณ€๊ฒฝ
    on_bridge = deque([0]*bridge_length)    # ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํŠธ๋Ÿญ ์ˆ˜ ๋งŒํผ deque ์ƒ์„ฑ
    cnt_time = 0    # ์‹œ๊ณ„
    sum_weight = 0  # ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ์™€์žˆ๋Š” ํŠธ๋Ÿญ ๋ฌด๊ฒŒ ํ•ฉ

    while truck_weights:    # ์˜ฌ๋ผ๊ฐˆ ํŠธ๋Ÿญ์ด ๋‚จ์•„ ์žˆ๋‹ค๋ฉด
        cnt_time += 1   # ์‹œ๊ฐ„ ์ฆ๊ฐ€
        out = on_bridge.popleft()
        sum_weight -= out
        if sum_weight + truck_weights[0] <= weight:  # ๋‹ค๋ฆฌ์— ํŠธ๋Ÿญ์ด ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฉด
            truck = truck_weights.popleft()
            on_bridge.append(truck) # ๋‹ค๋ฆฌ์— ํŠธ๋Ÿญ ์ถ”๊ฐ€
            sum_weight += truck     # ํŠธ๋Ÿญ ๋ฌด๊ฒŒ ํ•ฉ ์ถ”๊ฐ€
        else:
            on_bridge.append(0)

    return cnt_time + bridge_length

์—ฐ์†ํ•ด์„œ ๋ชจ๋“  ํŠธ๋Ÿญ์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” 

๋‹ค๋ฆฌ์˜ ๊ธธ์ด bridge_length + ํŠธ๋Ÿญ์˜ ๊ฐœ์ˆ˜ len(truck_weights) ๋ฅผ return ํ•˜๋ฉด ๋œ๋‹ค.

 

๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ,

์˜ฌ๋ผ๊ฐˆ ํŠธ๋Ÿญ์ด ๋‚จ์•„์žˆ๊ณ  ์ƒˆ๋กœ์šด ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ์— ์ถ”๊ฐ€๋˜์–ด๋„ ๊ฐ€๋Šฅํ•œ ์ƒํ™ฉ์—์„œ

ํŠธ๋Ÿญ๋ณ„ ๋ฌด๊ฒŒ๊ฐ€ ์ €์žฅ๋œ truck_weight์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด on_bridge์— ์ถ”๊ฐ€ํ•œ๋‹ค.

on_bridge๋Š” ๋‹ค๋ฆฌ ์œ„์˜ ์ƒํ™ฉ์„ ์˜๋ฏธํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ์ดˆ๊ธฐ๊ฐ’์€ [0]*๋‹ค๋ฆฌ์˜ ๊ธธ์ด bridge_length์ด๋‹ค.

 

๋งˆ์ง€๋ง‰์— cnt_time์— bridge_length๋ฅผ ๋”ํ•ด์ฃผ๋Š” ์ด์œ ๋Š”

๋งˆ์ง€๋ง‰์— ์˜ฌ๋ผ์˜จ ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๋‹ค ์ง€๋‚˜๊ฐˆ ์‹œ๊ฐ„์„ ๋”ํ•ด์ฃผ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

728x90

BELATED ARTICLES

more