๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail
 

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

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

programmers.co.kr

๋ฌธ์ œ ์„ค๋ช…

 

 

๋ฌธ์ œ ํ’€์ด

์ถฉ์‹คํžˆ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋‹ค.

 

์œ ์˜ํ•ด์•ผ ํ•  ์ ์€ 

 

1. ๋งˆ์ง€๋ง‰์— ๋‚˜๊ฐ€์ง€ ์•Š์€ ์ฐจ๋Š” 23:59๋ถ„์— ์ถœ์ฐจํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

2. ์ดˆ๊ณผ์‹œ๊ฐ„ ๊ณ„์‚ฐํ•  ๋•Œ ์˜ฌ๋ฆผํ•œ๋‹ค. ex) 40๋ถ„ ์ดˆ๊ณผํ–ˆ๋Š”๋ฐ 30๋ถ„๋งˆ๋‹ค 1000์› ์ถ”๊ฐ€๋ฉด 2000์› ์ถ”๊ฐ€

 

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

[Int: (isOut: Bool, time: Int)]

[์ฐจ๋ฒˆํ˜ธ: (๋‚˜๊ฐ”๋Š”์ง€, ์‚ฌ์šฉ์‹œ๊ฐ„)]

 

๋งˆ์ง€๋ง‰์— ๋‚˜๊ฐ”๋Š”์ง€ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ์˜ value์— isOut์„ ์ถ”๊ฐ€ํ•œ ํ˜•ํƒœ๋‹ค.

 

records๋ฅผ ๋‹ค ์ฒดํฌํ•˜๊ณ  ๋งŒ์•ฝ์— ์•„์ง ์•ˆ๋‚˜๊ฐ„ ์ฐจ๊ฐ€ ์žˆ๋‹ค๋ฉด(isOut์ด false์ด๋ฉด) 

23:59๋ถ„์— ๋‚˜๊ฐ”๋‹ค๊ณ  ๊ฐ„์ฃผํ•˜๊ณ  ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•œ๋‹ค.

 

๊ทธ ๋‹ค์Œ์—” ์ฐจ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ๊ณ  ์š”๊ธˆ ๊ณ„์‚ฐํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

์‹œ๊ฐ„์€ ๋ถ„์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

ex) 01:10์ด๋ฉด 70 ๋ฆฌํ„ดํ•ด์คŒ

 

 

์ฝ”๋“œ

import Foundation

func solution(_ fees:[Int], _ records:[String]) -> [Int] {
    
    // ์‹œ๊ฐ„์„ ๋ถ„์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ฉ”์†Œ๋“œ ex) 01:10 -> 70
    func timeToMinute(_ time: String) -> Int {
        let split = time.split(separator: ":").map {Int(String($0))!}
        return 60*split[0] + split[1]
    }
    
    // [์ฐจ๋ฒˆํ˜ธ: (๋‚˜๊ฐ”๋Š”์ง€, ์‚ฌ์šฉ์‹œ๊ฐ„)]
    var dict: [Int: (isOut: Bool, time: Int)] = [:]
    
    for record in records {
        let split = record.split(separator: " ").map {String($0)}
        let time = split[0]
        let carNumber = Int(split[1])!
        
        // ์ฐจ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด
        if split[2] == "IN" {
            if dict[carNumber] == nil {
                dict[carNumber] = (false, timeToMinute(time))
            }
            else {
                let usageTime = dict[carNumber]!.time + timeToMinute(time)
                dict[carNumber] = (false, usageTime)
            }
        }
        
        // ์ฐจ๊ฐ€ ๋‚˜๊ฐ€๋ฉด isOut true๋กœ ๋ณ€๊ฒฝ
        else if split[2] == "OUT" {
            let usageTime =  dict[carNumber]!.time - timeToMinute(time)
            dict[carNumber] = (true, usageTime)
        }
    }
    
    // ์•„์ง ์•ˆ๋‚˜๊ฐ„ ์ฐจ๊ฐ€ ์žˆ๋‹ค๋ฉด 23:59๋ถ„์— ๋‚˜๊ฐ”๋‹ค๊ณ  ๊ฐ€์ •
    for (key, value) in dict {
        if !dict[key]!.isOut {
            let usageTime = dict[key]!.time - timeToMinute("23:59")
            dict[key] = (false, usageTime)
        }
    }
    
    // ์ฐจ ๋ฒˆํ˜ธ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    let dictSorted = dict.sorted { $0.key < $1.key }
    var answer: [Int] = []
    
    for (key, value) in dictSorted {
        let time = value.time*(-1)
        
        // ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ๋„˜๊ธฐ์ง€ ์•Š์•˜์œผ๋ฉด ๊ธฐ๋ณธ ์š”๊ธˆ๋งŒ
        if time <= fees[0] {
            answer.append(fees[1])
        }
        
        // ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ๋„˜๊ฒผ๋‹ค๋ฉด
        else {
            let c = ceil((Float(time)-Float(fees[0])) / Float(fees[2]))
            let money = fees[1] + Int(c)*fees[3]
            answer.append(money)
        }
    }
    return answer
}

 

 

ํ›„๊ธฐ

ํ’€์ด์‹œ๊ฐ„: 1์‹œ๊ฐ„

 

๊ทธ๋ƒฅ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ธ๋ฐ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค..

 

๋‚˜๋ˆ„๊ณ  ์˜ฌ๋ฆผ์€ ceil, ๋ฐ˜์˜ฌ๋ฆผ round, ๋‚ด๋ฆผ์€ floor ์™ธ์šฐ์ž

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...