Coding/Code in the Morning

210127_Code in the morning (๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ) ๋‹ค์‹œ ํ’€ ๊ฒƒ

๊น€์œ ๋‹ˆ์ฝ˜ 2022. 1. 27. 16:50

Code in the morning, ๋งค์ผ ์•„์นจ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค. 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

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

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

 

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

 

 

๐Ÿ“ ์ œํ•œ ์‚ฌํ•ญ

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

 

 

๐Ÿ’ก ๋‚˜์˜ ํ’€์ด 

def solution(bridge_length,weight,truck_weights):
    answer = 0
    bridge_on = [0]* bridge_length
    curr_weight = 0
    
    while truck_weights:
        answer+=1
        bridge_out = bridge_on.pop(0)
        curr_weight -= bridge_out

        if curr_weight + truck_weights[0] > weight:
            bridge_on.append(0)
        else:
            truck = truck_weights.pop(0)
            bridge_on.append(truck)
            curr_weight += truck


    while curr_weight>0:
        answer +=1
        bridge_out = bridge_on.pop(0)
        curr_weight -=bridge_out

    return answer

 

 

๐Ÿ‘‰๐Ÿป ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ 

 

* ๋ฌธ์ œ ํ•ด์„ค ์ฐธ๊ณ  : https://assaeunji.github.io/python/2020-05-08-pgtruck/