๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ Swift

by Fomagran ๐Ÿ’ป 2020. 3. 19.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

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

100

100

[10]

101

100

100

[10,10,10,10,10,10,10,10,10,10]

110

2

10

[7,4,5,6]

8

ํ’€์ด:

 

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์Šคํƒ/ํ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ์ œ๊ฐ€ ์ƒ๊ฐํ–ˆ์„ ๋•Œ ํŠธ๋Ÿญ์ด ์˜ฌ๋ผํƒˆ ๋•Œ์˜ ์‹œ๊ฐ„์„ ๊ธฐ์–ตํ•˜๋Š” ๊ฒƒ์ธ๋ฐ์š”.

 

๋จผ์ € ์‹œ๊ฐ„์„ ์žด ๋ณ€์ˆ˜์™€  -> var time = 0

 

๋Œ€๊ธฐ์ค‘์ธ ํŠธ๋Ÿญ์„ ๋‚˜ํƒ€๋‚ผ ๋ณ€์ˆ˜ -> var wait = truck_weights

 

๋ฌด๊ฒŒ๋ฅผ ์žด ๋ณ€์ˆ˜ -> var times = [Int]()

 

๊ฐ ํŠธ๋Ÿญ์ด ๋‚ด๋ฆด ๋•Œ ์‹œ๊ฐ„์„ ๋‹ด๋Š” ๋ณ€์ˆ˜ -> var times = [Int]()

 

ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ธฐ์–ตํ•  ๋ณ€์ˆ˜ -> var weights = [Int]()

 

์ด๋ฒˆ ๋ฌธ์ œ๋Š” while๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ wait ์ฆ‰ ๋Œ€๊ธฐ ์ค‘์ธ ํŠธ๋Ÿญ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.  ->while !wait.isEmpty{...}

 

๊ทธ๋ฆฌ๊ณ  ์‹œ๊ฐ„์„ ๋ฐ˜๋ณต์ด ๋  ๋•Œ๋งˆ๋‹ค 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ์ค๋‹ˆ๋‹ค. ->  time += 1

 

๊ทธ ๋‹ค์Œ ์‹œ๊ฐ„์ด ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ์ฆ‰ ์˜ฌ๋ผํƒ„ ํŠธ๋Ÿญ์ด ์žˆ๋‹ค๋ฉด (์™œ ์˜ฌ๋ผํƒ„ ํŠธ๋Ÿญ์ธ์ง€๋Š” ๋’ค์— ์„ค๋ช…) ->  if !times.isEmpty{...)

 

๋น„์–ด์žˆ์ง€ ์•Š๊ณ  ๋งŒ์•ฝ ํ˜„์žฌ ์‹œ๊ฐ„์ด times(ํŠธ๋Ÿญ์ด ๋‚ด๋ฆด ๋•Œ์˜ ์‹œ๊ฐ„)๊ณผ ๊ฐ™๋‹ค๋ฉด -> if time == times.first! {...}

 

times์•ˆ์— ๋งจ ์ฒซ๋ฒˆ์งธ๋ฅผ ์‚ญ์ œํ•ด์ฃผ๊ณ  w์—์„œ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋‹ด์€ ๊ฒƒ (weights)์ค‘ ๋งจ ์ฒซ๋ฒˆ์งธ๋งŒํผ ๋นผ์ฃผ๊ณ  ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋‹ด์€ ๊ฒƒ(weights) ์ค‘ ๋งจ ์ฒซ๋ฒˆ์งธ๋ฅผ ์‚ญ์ œํ•ด์ค๋‹ˆ๋‹ค. -> times.removeFirst() w -= weights.first! weights.removeFirst()

 

๊ทธ ๋‹ค์Œ ๋งŒ์•ฝ ๋ฌด๊ฒŒ๊ฐ€ ๋Œ€๊ธฐ ์ค‘์ธ ํŠธ๋Ÿญ์ด ๋“ค์–ด์™€๋„ ๋ฒ„ํ‹ธ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ->  if w+wait.first! <= weight{...}

 

w์— ๋Œ€๊ธฐ์ค‘์ธ ํŠธ๋Ÿญ์˜ ๋งจ ์ฒซ๋ฒˆ์งธ ๋ฌด๊ฒŒ๋ฅผ ๋”ํ•ด์ฃผ๊ณ  ->  _> w += wait.first!

 

weights๋ฐฐ์—ด์— ๋Œ€๊ธฐ์ค‘์ธ ํŠธ๋Ÿญ์˜ ๋งจ ์ฒซ๋ฒˆ์งธ ๋ฌด๊ฒŒ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  -> weights.append(wait.first!)

 

times์— ํ˜„์žฌ์‹œ๊ฐ„์— ๋‹ค๋ฆฌ๊ธธ์ด๋ฅผ ํ•ฉ์นœ (์ด๊ฒŒ ๋ฐ”๋กœ ํŠธ๋Ÿญ์ด ๋‚ด๋ฆด ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ) ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. -> times.append(time+bridge_length)

 

๊ทธ ๋‹ค์Œ ๋Œ€๊ธฐ์—ด์—์„œ ๋งจ ์ฒซ๋ฒˆ์งธ ํŠธ๋Ÿญ์„ ์‚ญ์ œํ•ด์ค๋‹ˆ๋‹ค. -> wait.removeFirst()

 

์ด๋ ‡๊ฒŒ wait๊ฐ€ ๋ชจ๋‘ ๋‹ค ์—†์–ด์งˆ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด times์—” ๋งˆ์ง€๋ง‰์œผ๋กœ ์˜ฌ๋ผํƒ„ ํŠธ๋Ÿญ์ด ๋‚ด๋ฆด ์‹œ๊ฐ„๋งŒ ๋‚จ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‚ด๋ฆฐ ๋’ค ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ๊ณ ๋กœ ์‹œ๊ฐ„์„ ๋‹ด์€ times ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰๋ฒˆ ์งธ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. -> return times.last!

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Foundation
 
func solution(_ bridge_length:Int, _ weight:Int, _ truck_weights:[Int]) -> Int {
    var time = 0
    var wait = truck_weights
    var w = 0
    var times = [Int]()
    var weights = [Int]()
    
    while !wait.isEmpty{
        time += 1
        if !times.isEmpty{
            if time == times.first! {
            times.removeFirst()
            w -= weights.first!
            weights.removeFirst()
        }
    }
        if w+wait.first! <= weight{
            w += wait.first!
            weights.append(wait.first!)
            times.append(time+bridge_length)
            wait.removeFirst()
        }
    }
    return times.last!
}

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€