์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 28 | 29 | 30 | 31 |
Tags
- Lv2
- SwiftUI
- ํ๋ก๊ทธ๋๋จธ์ค
- UIKit
- ํจ์คํธ์บ ํผ์ค
- BOJ
- TCA
- XCTest
- tableView
- RxSwift
- realm
- MVVM
- BFS
- Flutter
- swift
- Swfit
- node.js
- ios
- rxcocoa
- raywenderlich
- CollectionView
- ๋ฐฑ์ค
- visionOS
- arkit
- Xcode
- SnapKit
- reactorkit
- combine
- designpattern
- Kuring
Archives
- Today
- Total
lgvv98
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ ๋ณธ๋ฌธ
์ฝ๋ฉํ
์คํธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ
๐ฅ ์บ๋ฟ๋งจ 2022. 4. 16. 02:10ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ
โ ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ
2022 KAKAO BLIND RECRUTMENT ๋ฌธ์ ๋ค!
์นด์นด์ค ๋ฌธ์ ๋ฅผ ํ๋๋ ๊ทธ๋ฅ ์ด๋ฆ๋ง์ผ๋ก๋ ๋ ์ง๋ฆฟํด.
โ ์ฝ๋
๋ ๋ฒจ์ 2์ง๋ง ํ์์ ๋ ์ ์๋ 1์ ๋ง ์ฃผ๋๋ผ.
๊ทธ๋งํผ ์ฌ์ ๋ค๋ ๋ง์ด๊ฒ ์ง ใ ใ ๊ทผ๋ฐ ์ง์ง ์ฝ๊ธด ํ๋ค...
import Foundation
func solution(_ fees:[Int], _ records:[String]) -> [Int] {
var dict: [String: String] = [:] // ์ฐจ๋๋ฒํธ : ์๊ฐ
var totalTimeArray: [String: Int] = [:] // ์ฐจ๋๋ฒํธ : ๋์ ์๊ฐ
var priceDict: [String: Int] = [:] // ์ฐจ๋๋ฒํธ : ๊ธ์ก
// records ๋ด์์์ ์ธ๋ฑ์ค์ ์ฐจ๋๋ฒํธ๋ฅผ ์ ์ฅ
records.forEach { record in
let info = record.components(separatedBy: " ")
let carNumber = info[1]
if dict[carNumber] == nil { // ์์ง INํ์ง ์์๋ค๋ฉด
dict[carNumber] = info[0]
if totalTimeArray[carNumber] == nil {
totalTimeArray[carNumber] = 0
}
} else { // ์ด๋ฏธ ์ฐจ๊ฐ ๋ค์ด์๋ค๋ฉด
// ๊ณ์ฐํ๋ ๋ก์ง ์ํํด์ผ ํฉ๋๋ค.
let inTime = dict[carNumber]! // ์ด์ ์ ๋ค์ด์๋ ์๊ฐ
let time = calueteTime(fees: fees, inTime: inTime, outTime: info[0]) // ์ด๋ฒ ๊ตฌ๊ฐ์์ ์ฃผ์ฐจํ ์๊ฐ ๊ณ์ฐ
dict[carNumber] = nil // ์ถ์ฐจ ์ฒ๋ฆฌ
if totalTimeArray[carNumber] == nil {
totalTimeArray[carNumber] = time
} else {
totalTimeArray[carNumber]! += time
}
}
}
dict.forEach { carNumber, value in
let inTime = dict[carNumber]!
let time = calueteTime(fees: fees, inTime: inTime, outTime: "23:59")
totalTimeArray[carNumber]! += time
dict[carNumber] = nil // ๊ตณ์ด ์ด ์ฝ๋๋ ์์ด๋ ๋๋ค.
}
totalTimeArray.forEach { carNumber, time in
let time = time - fees[0] // ๊ธฐ๋ณธ์๊ฐ ๋นผ์ฃผ๊ธฐ
var price = fees[1] // ๊ธฐ๋ณธ๊ธ์ก
if time <= 0 {
// ๊ธฐ๋ณธ ๊ธ์ก๋ง ๋ด๋ฉด ๋๋ค.
} else {
var count = time / fees[2]
if time % fees[2] != 0 {
count += 1
}
price += count * fees[3]
}
priceDict[carNumber] = price
}
var answer = priceDict.sorted { $0.key < $1.key }.map { $0.value }
return answer
}
func calueteTime(fees: [Int], inTime: String, outTime: String) -> Int{
let inTimeArray = inTime.components(separatedBy: ":").map { Int($0)! }
let outTimeArray = outTime.components(separatedBy: ":").map { Int($0)! }
let hour = outTimeArray[0] - inTimeArray[0]
let minute = outTimeArray[1] - inTimeArray[1]
return hour * 60 + minute
}
'์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. [3์ฐจ] ํ์ผ๋ช ์ ๋ ฌ (0) | 2022.04.16 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ๋ฐฉ๋ฌธ ๊ธธ์ด (0) | 2022.04.16 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ํฐ ์ ๋ง๋ค๊ธฐ (4) | 2022.04.13 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค LV2. ์ฟผ๋ ์์ถ ํ ๊ฐ์ ์ธ๊ธฐ (0) | 2022.04.13 |
[Swift] BOJ 1753 ์ต๋จ๊ฒฝ๋ก (0) | 2022.04.13 |
Comments