λ¬Έμ μ€λͺ
λͺκ°μ κ°μ€μ΄ νμνμ§ κ΅¬νλ λ¬Έμ
ν κ°μ€μ μ΄μ΄μ μ¬μ©νλ €λ©΄ μ²μμκ° 10λΆμ λν΄μΌ νλ€.
λ¬Έμ νμ΄
1. μμ½ μμ μκ° κΈ°μ€μΌλ‘ μ λ ¬νλ€.
2. μκ°μ λΆμΌλ‘ κ³μ°νλ€. 10:10 -> 10*60 + 10 = 610
3. κ°μ€ λ²νΈλ₯Ό λ°°μ νκ³ λ§μ½ λ°°μ λ μμ½μκ°μ λλλ μκ°+10λ³΄λ€ μμ½ μμμκ°μ΄ λμ€μ΄λ©΄ κ·Έ κ°μ€μ μ΄μ΄ μ¬μ©νλ€.
import Foundation
func solution(_ book_time:[[String]]) -> Int {
var answer = 0
let bookTime = book_time.sorted(by: {$0[0] < $1[0]})
var bookNum = Array(repeating: 0, count: bookTime.count) // κ°μ€λ²νΈ ν λΉ
let bookTimeToMinutes = timeToMinutes(bookTime)
var endTime = 0
for i in 0..<bookTimeToMinutes.count {
// κ°μ€λ²νΈκ° ν λΉλμ§ μμμΌλ©΄
if bookNum[i] == 0 {
answer += 1
bookNum[i] = answer
endTime = bookTimeToMinutes[i][1]+10
}
// μ΄λ―Έ ν λΉλ κ°μ€μ΄λ©΄ ν¨μ€
else {
continue
}
for j in i+1..<bookTimeToMinutes.count {
// κ·Έ μμ½μ κ°μ€μ΄ ν λΉλμ§ μμκ³ && νμ¬ κ°μ€μ΄ λλλ μκ° μ΄νμ μ¬μ©ν μ μλ€λ©΄
if bookNum[j] == 0 && endTime <= bookTimeToMinutes[j][0] {
bookNum[j] = answer
endTime = bookTimeToMinutes[j][1]+10
}
}
}
return answer
}
// λΆμΌλ‘ κ³μ°νλ λ©μλ ex) 10:10 -> 10*60 + 10 = 610
func timeToMinutes(_ bookTime: [[String]]) -> [[Int]] {
var minutesArray = [[Int]]()
for times in bookTime {
var arr = [Int]()
for time in times {
let split = time.split(separator: ":").map {Int(String($0))!}
arr.append(split[0]*60 + split[1])
}
minutesArray.append(arr)
}
return minutesArray
}