๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ Programmers(Lv.2)

by seolhee2750 2021. 8. 6.
๋ฌธ์ œ ์„ค๋ช…

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

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import Foundation

func solution(_ bridge_length:Int, _ weight:Int, _ truck_weights:[Int]) -> Int {
    var stack = Array(repeating: 0, count: bridge_length)
    var truck = truck_weights
    var sec = 0
    var now_weight = 0

    // ๋ฐ˜๋ณต๋งˆ๋‹ค ํŠธ๋Ÿญ์˜ ๋ฐฐ์—ด์„ ์‚ญ์ œ
    while !stack.isEmpty {
        sec += 1
        now_weight -= stack.removeFirst()

        // ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ. ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ํŠธ๋Ÿญ์ด ์žˆ์„ ๋•Œ๋งŒ ๊ณ„์‚ฐ
        if let t = truck.first {
            if t + now_weight > weight {
                stack.append(0)
            }
            else {
                stack.append(t)
                now_weight += truck.removeFirst()
            }
        }
    }
    return sec
}
  • bridge_length๋งŒํผ 0์„ ์ฑ„์šด stack์„ ๋งŒ๋“ค์—ˆ๋‹ค. (๋‹ค๋ฆฌ๋ฅผ ์˜๋ฏธ)
  • while๋ฌธ์œผ๋กœ, stack์ด ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋™์•ˆ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณตํ–ˆ๋‹ค.
  • ํ•œ ๋ฒˆ์˜ ๋ฐ˜๋ณต๋งˆ๋‹ค sec์— 1์„ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.
    (์ƒˆ๋กœ์šด ํŠธ๋Ÿญ์ด ๋“ค์–ด๊ฐ€๋“  ์•ˆ๋“ค์–ด๊ฐ€๋“  ํŠธ๋Ÿญ์˜ ์›€์ง์ž„์€ ๊ณ„์† ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ.)
  • now_weight ๋ณ€์ˆ˜๋Š” ํ˜„์žฌ ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์žˆ๋Š” ํŠธ๋Ÿญ ๋ฌด๊ฒŒ์˜ ํ•ฉ์„ ์˜๋ฏธํ•˜๋Š”๋ฐ,
    ๋ฐ˜๋ณต๋งˆ๋‹ค 0์ด stack์—์„œ pop๋ ์ˆ˜๋„ ์žˆ๊ณ , ์–ด๋– ํ•œ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๊ฐ€ pop๋  ์ˆ˜๋„ ์žˆ์ง€๋งŒ
    ๊ตณ์ด if๋ฌธ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ณ„์† pop๋˜๋Š” ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ now_weight์—์„œ ๋นผ์ฃผ์—ˆ๋‹ค.
  • if let ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์•„์ง ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์€ ํŠธ๋Ÿญ์ด ์กด์žฌํ•  ๋•Œ๋งŒ ์ˆ˜ํ–‰๋˜๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.
    ๋Œ€๊ธฐ์ค‘์ธ ํŠธ๋Ÿญ๊ณผ now_weight์˜ ๋ฌด๊ฒŒ ํ•ฉ์ด weight๋ณด๋‹ค ํด๋•Œ๋Š” ๋‹ค๋ฆฌ์—(stack์—) 0์„ ์˜ฌ๋ ค์ฃผ์—ˆ๊ณ ,
    ์ž‘์„๋•Œ๋Š” ๋‹ค๋ฆฌ์—(stack์—) ํŠธ๋Ÿญ์„ ์˜ฌ๋ ค์ค€ ํ›„ now_weight์— ๋ฐฉ๊ธˆ ์˜ฌ๋ผ๊ฐ„ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ํ•ฉ์‚ฐํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ๊ตฌ์„ฑํ•˜๋‹ˆ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง„ ๊ฒƒ ๊ฐ™๋‹ค.
    ์ฒ˜์Œ์—๋Š” t๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๋Œ€๊ธฐ ํŠธ๋Ÿญ์ด ์—†์„ ๋•Œ๋ฅผ ๋˜ ๋”ฐ๋กœ ๋นผ์ค˜์•ผํ•ด์„œ ๋ณต์žกํ–ˆ๋‹ค.
  • ์ฝ”๋“œ๋„ ์งง๊ณ  ์‰ฌ์šด ๋ฌธ์ œ๊ฐ™์ง€๋งŒ,, ์ •๋ง์ •๋ง ์˜ค๋žœ์‹œ๊ฐ„ ๊ฑธ๋ ค ํ‘ผ ๋ฌธ์ œ๋ผ๋Š” ์‚ฌ์‹ค,,
    ์™œ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๋„ ์ž˜ ๋œจ๊ณ  ์ž‘์€ ๋ณ€๊ฒฝํ•˜๋‚˜์— ๋‹น๋ฝ์ด ๊ฒฐ์ •๋˜๋Š” ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.
    ๋‚˜์ค‘์— ๊ผญ ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด์ž...

 


 

 

๋ฌธ์ œ

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

๋Œ“๊ธ€