Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6578ee8

Browse files
authored
Create calculateParkFees.swift
1 parent 8e5abe0 commit 6578ee8

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import Foundation
2+
3+
/*
4+
두번째 try 걸린시간 50min..
5+
어려웠던 점 : 변수를 재사용할 때 오타 이름을 사용하는 경우가 많았음... 디버깅이 안되서 index out of range 체크를 하지 못했음...
6+
그중 하나가 클래스에 append하는 경우인데 맴버변수의 배열을 빈 배열로 초기화 했기에 append를 해야했는데
7+
index 라는 변수를 추가시키면서 아직 원소가 빈 자리에 index를 활용해서 에러가 났음.
8+
추가적으로 문제에선 차 넘버가 낮은 순으로 출력하라는 것을 뒤늦게 확인했음..
9+
*/
10+
// feeds[0] == 기본 시간
11+
// feeds[1] == 기본 요금
12+
// feeds[2] == 추가 시간
13+
// feeds[3] == 추가 시간당 요금
14+
15+
class UserCarInfo {
16+
//변수
17+
var visited = [Bool]()
18+
var visitedTime = [Int]()
19+
var totalFee = 0
20+
var visitedMinutes = 0
21+
var number = ""
22+
var index = 0
23+
}
24+
25+
//함수
26+
extension UserCarInfo {
27+
28+
func configure(time: String, num: String) {
29+
number = num
30+
changeTimeToMinutesWithInt(time: time)
31+
checkVisited()
32+
index += 1
33+
}
34+
//input시 시간을 숫자 분으로 바꿈
35+
func changeTimeToMinutesWithInt(time: String) {
36+
let separated = time.split(separator:":").map{Int(String($0))!}
37+
38+
visitedTime.append(separated[0] * 60 + separated[1])
39+
}
40+
//inoutCheck
41+
func checkVisited() {
42+
if index % 2 == 0 {
43+
visited.append(true)
44+
return
45+
}
46+
visited.append(false)
47+
}
48+
}
49+
50+
var carList = [UserCarInfo]()
51+
52+
func setupCarList(_ records: [String]) {
53+
54+
records.forEach { record in
55+
var isNewParkedCarID = false
56+
let timeNumberIn = record.split(separator: " ").map{String($0)}
57+
for car in carList {
58+
if car.number == timeNumberIn[1] {
59+
car.configure(time: timeNumberIn[0], num: timeNumberIn[1])
60+
isNewParkedCarID = true
61+
break
62+
}
63+
}
64+
if !isNewParkedCarID {
65+
let newUserCar = UserCarInfo()
66+
newUserCar.configure(time: timeNumberIn[0], num: timeNumberIn[1])
67+
carList.append(newUserCar)
68+
}
69+
}
70+
}
71+
72+
func userFeesCalculate(_ fees: [Int])-> [Int] {
73+
carList.forEach { car in
74+
for i in 0..<car.visited.count {
75+
if car.visited[i] {
76+
continue
77+
}
78+
car.visitedMinutes += car.visitedTime[i] - car.visitedTime[i-1]
79+
}
80+
if car.visited.last! {
81+
car.visitedMinutes += 23*60 + 59 - car.visitedTime.last!
82+
}
83+
if car.visitedMinutes - fees[0] <= 0 {
84+
car.totalFee = fees[1]
85+
return
86+
}
87+
88+
var lastMinutes = car.visitedMinutes - fees[0]
89+
if lastMinutes%fees[2] != 0 {
90+
lastMinutes = lastMinutes/fees[2] + 1
91+
}else {
92+
lastMinutes = lastMinutes/fees[2]
93+
}
94+
car.totalFee += fees[1] + lastMinutes*fees[3]
95+
}
96+
carList.sort { $0.number < $1.number }
97+
return carList.map{$0.totalFee}
98+
}
99+
100+
101+
func solution(_ fees:[Int], _ records:[String]) -> [Int] {
102+
103+
setupCarList(records)
104+
return userFeesCalculate(fees)
105+
106+
}

0 commit comments

Comments
 (0)