-
Notifications
You must be signed in to change notification settings - Fork 3
/
회의실 배정.swift
40 lines (33 loc) · 916 Bytes
/
회의실 배정.swift
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
32
33
34
35
36
37
38
39
40
import Foundation
let N = Int(readLine()!)!
var arr = [[Int]]()
(0..<N).forEach { _ in
let meeting = readLine()!.split(separator: " ").map { Int(String($0))! }
arr.append(meeting)
}
func solution(arr: [[Int]]) -> Int {
let sortedArr = arr.sorted {
if $0[0] < $1[0] {
return true
} else if $0[0] == $1[0] {
return $0[1] < $1[1]
} else {
return false
}
}
var count = 0
var index = 1
var lastMeeting = sortedArr.first!
while index < sortedArr.count {
defer { index += 1 }
let currentMeeting = sortedArr[index]
if currentMeeting[1] < lastMeeting[1] {
lastMeeting = currentMeeting
} else if lastMeeting[1] <= currentMeeting[0] {
count += 1
lastMeeting = currentMeeting
}
}
return count + 1
}
print(solution(arr: arr))