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

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ 3 ๊ธˆ๊ณผ ์€ ์šด๋ฐ˜ํ•˜๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 10. 10.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ธˆ๊ณผ ์€ ์šด๋ฐ˜ํ•˜๊ธฐ

์–ด๋Š ์™•๊ตญ์— ํ•˜๋‚˜ ์ด์ƒ์˜ ๋„์‹œ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์™•๊ตญ์˜ ์™•์€ ์ƒˆ ๋„์‹œ๋ฅผ ์ง“๊ธฐ๋กœ ๊ฒฐ์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋„์‹œ๋ฅผ ์ง“๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋„์‹œ๋ฅผ ์ง“๋Š” ์žฅ์†Œ์— ๊ธˆ a kg๊ณผ ์€ b kg์ด ์ „๋‹ฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋„์‹œ์—๋Š”

programmers.co.kr


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ด์ง„ํƒ์ƒ‰์„ ์ด์šฉํ•ด์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

์ตœ๋Œ€๋กœ ๊ฑธ๋ฆด ์‹œ๊ฐ„์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ ๊ฐ€๋ฉด์„œ ๊ธˆ๊ณผ ์€์„ ์šด๋ฐ˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค.

 

1. ์ตœ๋Œ€๋กœ ๊ฑธ๋ฆด ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.

 

์•„๋ž˜ ์ œํ•œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜๋ฉด a์™€ b์˜ ์ตœ๋Œ€์น˜๊ฐ€ ๊ฐ ๊ฐ 10์˜ 9์Šน์ด๋ฏ€๋กœ  10์˜ 9์Šน * 2

 

์›€์ง์ด๋Š”๋ฐ ์ตœ๋Œ€๋กœ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด 10์˜ 5์Šน์ด๊ณ  ์™”๋‹ค ๊ฐ”๋‹ค ์™•๋ณต์ด๋ฏ€๋กœ 10์˜ 5์Šน *2

 

์ฆ‰, 10e9 * 10e5 * 4์ž…๋‹ˆ๋‹ค.

 

 

2. ์ด์ง„ ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๋ฉด์„œ ๊ด‘๋ฌผ์„ ์šด๋ฐ˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

๊ด‘๋ฌผ์„ ์šด๋ฐ˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ํŠธ๋Ÿญ์˜ ๊ด‘๋ฌผ์˜ ํ•ฉ์˜ ์ตœ๋Œ€์น˜๊ฐ€ ๋ชจ๋‘

 

a(ํ•„์š”ํ•œ ๊ธˆ์˜ ์–‘),b(ํ•„์š”ํ•œ ์€์˜ ์–‘),a+b(ํ•„์š”ํ•œ ๊ธˆ๊ณผ ์€์˜ ํ•ฉ)๋ณด๋‹ค ํฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค. (์—ฌ๊ธฐ ํ•ด์„ค์—์„œ ์ž์„ธํ•œ ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.)

 

๊ฐ ํŠธ๋Ÿญ์˜ ๊ด‘๋ฌผ์˜ ์–‘๊ณผ (์‹œ๊ฐ„ ๋‚ด์— ์šด๋ฐ˜ํ•  ์ˆ˜ ์žˆ๋Š” ํšŸ์ˆ˜ * ๋ฌด๊ฒŒ์˜์–‘ )์„ ๋น„๊ตํ•ด์„œ ๋” ์ž‘์€ ์ชฝ์ด ์ตœ๋Œ€์น˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ด์ง„ ํƒ์ƒ‰์„ ์ด์šฉํ•ด์„œ ๊ด‘๋ฌผ์„ ์šด๋ฐ˜ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.


Source Code

 


1์ฐจ ํ’€์ด

 

์ฒซ ํ’€์ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‹œ๊ฐ„์„ 1์ดˆ๋ถ€ํ„ฐ ์„ธ๋ฉด์„œ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ ๊ฐ’์ด ๋˜์—ˆ์„ ๋•Œ ๋ฆฌํ„ดํ•˜๋„๋ก ํ–ˆ๋‹ค.

 

๊ฒฐ๊ณผ๋Š”...ํ•˜๋‚˜ ๋บด๊ณ  ์‹คํŒจ (signal: illegal instruction (core dumped)) ๋ผ๊ณ  ๋–ด๋‹ค... 

 

๋ญ... ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ  ๋‹ค ์œ„์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋– ์„œ ๋‹นํ™ฉํ–ˆ๋‹ค..

 

import Foundation

struct Truck {
    var gold:Int
    var silver:Int
    var weight:Int
    var time:Int64
}

func solution(_ a:Int, _ b:Int, _ g:[Int], _ s:[Int], _ w:[Int], _ t:[Int]) -> Int64 {
    
    var gold:Int = a
    var silver:Int = b
    var time:Int64 = 0
    var waitingTrucks:[Int64:[Truck]] = [:]
    
    for (i,time) in t.enumerated() {
        if waitingTrucks[Int64(time)] == nil {
            waitingTrucks[Int64(t[i])] = [Truck(gold: g[i], silver: s[i], weight: w[i], time: Int64(t[i]))]
        }else {
            waitingTrucks[Int64(time)]!.append(Truck(gold: g[i], silver: s[i], weight: w[i], time: Int64(t[i])))
        }
    }
        
    while true {
        time += 1
        if waitingTrucks[time] != nil {
            for truck in waitingTrucks[time]! {
                var newTruck:Truck = truck
                var weight:Int = truck.weight
                if gold > 0 {
                    gold -= truck.gold > 0 ? truck.gold > weight ? weight : truck.gold : 0
                    if truck.gold > 0 {
                        weight = gold < 0 ? weight + gold : weight - truck.gold
                    }
                    newTruck.gold -= weight
                    gold = gold < 0 ? 0 : gold
                }
                if weight > 0 {
                    silver -= truck.silver > 0 ? truck.silver > weight ? weight : truck.silver : 0
                    newTruck.silver -= weight
                }
                if waitingTrucks[time + truck.time*2] == nil {
                    waitingTrucks[time + truck.time*2] = [newTruck]
                }else {
                    waitingTrucks[time + truck.time*2]!.append(newTruck)
                }
                if gold <= 0 && silver <= 0 {
                    return time
                }
            }
        }
    }
}

๋Š๋‚€์ 

 

์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋จผ์ € ์‹์„ ์„ธ์šฐ๊ณ  ํ‘ธ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค...

 

1์ฐจ ํ’€์ด๋ฅผ ํ•˜๊ณ  ๋„์ €ํžˆ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘ธ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ํ•ด์„ค์„ ๋ดค๋Š”๋ฐ ์ด๋ถ„ํƒ์ƒ‰์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ด๋ถ„ํƒ์ƒ‰์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์•Œ๊ณ  ๋‚˜์„œ๋„ ๊ธˆ๊ณผ ์€์„ ์šด๋ฐ˜ํ•˜๋Š” ์‹ ์ž์ฒด๋ฅผ ๋– ์˜ฌ๋ฆฌ๊ธฐ๊ฐ€ ํž˜๋“ค์–ด์„œ ๋‚˜ํ•œํ…Œ๋Š”

 

๊ฝค ์–ด๋ ค์šด ๋ฌธ์ œ์˜€๋‹ค...


Reference

 

 

์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ3 9์›” ํ•ด์„ค

์ฝ”๋”ฉ์ด ์žฌ๋ฏธ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ ์ฑŒ๋ฆฐ์ง€! ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ 2021๋…„ 9์›” 9์ผ, 10์›” 7์ผ ๋‘ ๋ฒˆ์— ๊ฑธ์ณ ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ3๊ฐ€ ์ง„ํ–‰ ์ค‘ ์ž…๋‹ˆ๋‹ค. 2021๋…„ 9์›” 9์ผ 19์‹œ 30๋ถ„๋ถ€ํ„ฐ 22์‹œ 30๋ถ„๊นŒ์ง€ ์ง„ํ–‰๋œ ์‹œ์ฆŒ2

prgms.tistory.com

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€