Algorithms/Programmers

์Šคํƒ/ํ 1๋ฒˆ lv2

ํƒฑ์ ค 2021. 3. 5. 15:32

๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

 

๋ฌธ์ œ ์„ค๋ช…

ํŠธ๋Ÿญ ์—ฌ๋Ÿฌ ๋Œ€๊ฐ€ ๊ฐ•์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์ผ ์ฐจ์„  ๋‹ค๋ฆฌ๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์œผ๋กœ ๊ฑด๋„ˆ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š”์ง€ ์•Œ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋Ÿญ์€ 1์ดˆ์— 1๋งŒํผ ์›€์ง์ด๋ฉฐ, ๋‹ค๋ฆฌ ๊ธธ์ด๋Š” 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 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

bridge_length / weight / truck_weights / return

2 10 [7,4,5,6] 8
100 100 [10] 101
100 100 [10,10,10,10,10,10,10,10,10,10] 110
def solution(bridge_length, weight, truck_weights):
    time = 0
    q = [0] * bridge_length
    
    # ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ๋™์•ˆ
    while q:
        # ์‹œ๊ฐ„ 1์ดˆ ์ฆ๊ฐ€
        time += 1
        q.pop(0)
        # pop(0)์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ๊บผ๋‚ด๊ธฐ (๋‹ค๋ฆฌ ํ•œ ์นธ์”ฉ ๋ฐ€๋ฆผ)
        if truck_weights:
            if sum(q) + truck_weights[0] <= weight: # ๋‹ค๋ฆฌ ์œ„ ํŠธ๋Ÿญ์ด ํ•œ๊ณ„ weight ์•ˆ ๋„˜์œผ๋ฉด
                q.append(truck_weights.pop(0)) # ํŠธ๋Ÿญ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€
            else:
                q.append(0) # weight ๋„˜์œผ๋ฉด ๋‹ค๋ฆฌ ํ•œ ์นธ ๋” ์ฐจ์ง€
    return time

# bridge_length 2
# weight 10
# truck_weights [7,4,5,6]
# return 8

์œ„์ฒ˜๋Ÿผ ๋‹ค๋ฆฌ length๊ฐ€ 2๋ผ๋ฉด, [0,0] ์™€๊ฐ™์€ 0์ด ๋“ค์–ด๊ฐ„ ๋ฆฌ์ŠคํŠธ q๋ฅผ ๋งŒ๋“ค๊ณ 
[0, ํŠธ๋Ÿญ] -> [ํŠธ๋Ÿญ,0]-> [0,0] ์ด๋Ÿฐ์‹์œผ๋กœ ํŠธ๋Ÿญ์ด ๋“ค์–ด์˜ค๊ณ  ๋‚˜๊ฐ€๊ฒŒ ๊ตฌํ˜„ ํ•จ์œผ๋กœ์จ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

  • bridge์— ๋‹ค๋ฆฌ์˜ ๊ธธ์ด๋งŒํผ ์›์†Œ 0์œผ๋กœ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด time ์ƒ์„ฑ
  • 1์ดˆ๊ฐ€ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฆฌ๋Š” ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ ๋ฐ€๋ฆฌ๊ณ  ์ด๋ฏธ ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐ„ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ์™€ ๋‹ค์Œ ์˜ฌ๋ผ๊ฐˆ ์ฐจ๋ก€์˜ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ ํ•ฉ์ด ๋‹ค๋ฆฌ๊ฐ€ ์ˆ˜์šฉ ๊ฐ€๋Šฅํ•œ ๋ฌด๊ฒŒ๋ผ๋ฉด ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆผ
  • ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” 0 ์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ๋‹ค๋ฆฌ๋ฅผ ๊ณ„์† ์™ผ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋ƒ„
  • truck_weight์™€ bridge๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

๋ฐฐ์šด์ 

  • while, if๋ฌธ์˜ ์กฐ๊ฑด์— list๋ฅผ ๋„ฃ์„ ๊ฒฝ์šฐ list๊ฐ€ ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด True, ๋น„์–ด ์žˆ์œผ๋ฉด False๋กœ ์—ฐ์‚ฐํ•จ
  • append(0)ํ•˜๋ฉด ๋งจ ๋’ค์— ์ถ”๊ฐ€๋˜๊ณ 
  • pop(0)ํ•˜๋ฉด ๋งจ ์•ž์˜ ์›์†Œ๊ฐ€ ๋น ์ง
728x90