https://school.programmers.co.kr/learn/courses/30/lessons/92341
import math
def solution(fees, records):
car = {} #차량번호:[주차시간]
def time(t):
data = list(map(int, t.split(":")))
return data[0]*60 + data[1]
def calculate(t):
if t <= fees[0]:
return fees[1]
return fees[1] + math.ceil((t-fees[0])/fees[2]) * fees[3]
for record in records:
data = record.split(" ")
if data[1] not in car:
car[data[1]] = []
car[data[1]].append(time(data[0]))
answer = []
for c in sorted(car):
if len(car[c]) % 2 != 0:
car[c].append(time("23:59"))
total = 0
for i in range(0, len(car[c]), +2):
total += car[c][i+1] - car[c][i]
answer.append(calculate(total))
return answer
import Foundation
func solution(_ fees:[Int], _ records:[String]) -> [Int] {
func time(_ t: String) -> Int {
let data = t.split(separator: ":").map{ Int($0)! }
return data[0]*60 + data[1]
}
func calculate(_ t: Int) -> Int {
if t <= fees[0]{
return fees[1]
}
return fees[1] + Int(ceil(Float(t-fees[0])/Float(fees[2]))) * fees[3]
}
var car = [String:[Int]]()
for record in records{
let data = record.split(separator: " ").map{ String($0) }
if car[data[1]] == nil {
car[data[1]] = []
}
car[data[1]]!.append(time(data[0]))
}
var answer = [Int]()
for (c,_) in car.sorted(by: { $0.key < $1.key }) {
if car[c]!.count % 2 != 0 {
car[c]!.append(time("23:59"))
}
var total = 0
for i in stride(from: 0, to: car[c]!.count, by: +2){
total += car[c]![i+1] - car[c]![i]
}
answer.append(calculate(total))
}
return answer
}
[프로그래머스] 뒤에 있는 큰 수 찾기 (0) | 2024.01.03 |
---|---|
[프로그래머스] 단어 변환 (0) | 2024.01.02 |
[프로그래머스] 압축 (0) | 2023.12.26 |
[프로그래머스] 네트워크 (1) | 2023.12.25 |
[프로그래머스] k진수에서 소수 개수 구하기 (0) | 2023.12.22 |