Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

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

๊ฐ์ž ๐Ÿฅ” 2021. 9. 15. 22:49
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

https://programmers.co.kr/learn/courses/30/lessons/42583

 

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

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

programmers.co.kr

 

๋‚˜์˜ ํ’€์ด

  • ์ผ๋‹จ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆผ
  • ํŠธ๋Ÿญ ํ•˜๋‚˜๊ฐ€ ์ด๋™ํ• ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด 1์ดˆ๊ฐ€ ์ง€๋‚˜๊ณ , 1์ดˆ ์•ˆ์— ๋‘ ๋Œ€์˜ ํŠธ๋Ÿญ์€ ์›€์ง์ด์ง€ ์•Š์Œ
  • ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฆฌ ๊ธธ์ด๊ฐ€ 10์ด๋ฉด, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Œ.
  • ํŠธ๋Ÿญ ใ…๊ฐ€ ํ•ด๋‹น ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋ ค๋ฉด
    • 1์ดˆ [0, 0, 0, 0, 0, 0, 0, 0, 0, ใ…]
    • 2์ดˆ  [0, 0, 0, 0, 0, 0, 0, 0, ใ…, 0]
    • 3์ดˆ  [0, 0, 0, 0, 0, 0, 0, ใ…, 0, 0]
    • 4์ดˆ  [0, 0, 0, 0, 0, 0, ใ…, 0, 0, 0] .... ์ด๋ ‡๊ฒŒ ์ง€๋‚˜๊ฒŒ๋˜์„œ ๋‹ค๋ฆฌ๋ฅผ ๋ชจ๋‘ ์ง€๋‚˜๋Š”๋ฐ 10์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๊ฒŒ ๋จ.
  • ๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋„ 0์„ ๋„ฃ์–ด๊ฐ€๋ฉด์„œ ๋Œ๋ ค์คŒ.
def s(bridge_length, weight, truck_weights):
    
    crossing = [0] * bridge_length  #๊ฑด๋„ˆ๋Š”์ค‘ ์ด๊ฑฐ๋Š” weight ์กฐ๊ฑด์„ ๋‘ฌ์•ผํ•จ
    time = 0
    escape = []
    total_truck = sum(truck_weights)
    
    while True:
        time += 1
        temp = crossing.pop(0)
        
        if temp != 0 :
            escape.append(temp)
        else:
            pass
        
        if truck_weights:  #์•ˆ์— ๊ฐ’์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ์กฐ๊ฑด๋ฌธ ๋Œ์•„๊ฐ
                           #๋งŒ์•ฝ์— truck_weights์— ๊ฐ’์ด ์—†๋Š” ์ƒํƒœ๋ผ๋ฉด, 
                           #์‹œ๊ฐ„ +1 ํ•˜๊ณ , ์•ž์— pop๋งŒ ์ž˜ํ•ด์ฃผ๋ฉด ๋˜์„œ ์ƒ๊ด€์—†๊ตฌ๋‚˜ ์™€์šฐ 
            if sum(crossing) + truck_weights[0] <= weight:
                crossing.append(truck_weights.pop(0))
            else:
                crossing.append(0)
            
        if total_truck == sum(escape):
            break
        
    return time

 

๋‹ค๋ฅธ์‚ฌ๋žŒํ’€์ด

https://programmers.co.kr/learn/courses/30/lessons/42583/solution_groups?language=python3 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ‘ผ ํ’€์ด๊ฐ€ ์ธ์ƒ๊นŠ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ์‹œ๊ฐ„์˜ ํšจ์œจ์ด ๋–จ์–ด์งˆ๋•Œ deque๋ฅผ ํ™œ์šฉํ•ด์„œ ํ’€๋ฉด ์–ด๋–จ์ง€ ์ƒ๊ฐํ•ด๋ณด์•˜๋‹ค.

๋ฐ˜์‘ํ˜•