728x90
반응형
https://www.acmicpc.net/problem/1931
- 그리디 알고리즘
- 정렬
import Foundation
let N = Int(readLine()!)!
var meetings:[[Int]] = []
for _ in 0 ..< N {
var meeting:[Int] = []
meeting = readLine()!.split(separator: " ").map{Int(String($0))!}
meetings.append([meeting[0],meeting[1]])
}
let sorted = meetings.sorted(by: {( $0[1],$0[0]) < ($1[1],$1[0]) })
//print(sorted)
var count = 1
var i = 1
var prev = 0
while i < sorted.count {
if sorted[prev][1] <= sorted[i][0] {
count += 1
prev = i
}
i += 1
}
print(count)
/*
TEST CASE
2
1 1
0 1
==> 2
5
1 9
2 8
3 7
4 6
5 5
==>1
11
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14
==> 4
5
6 7
6 6
5 6
5 5
7 7
==>5
*/
728x90
반응형
'개발 > 코딩' 카테고리의 다른 글
백준 - 보물 - Swift (0) | 2022.07.10 |
---|---|
백준 - 동전 0 - Swift (0) | 2022.07.10 |
백준 - ATM - Swift (0) | 2022.07.10 |
백준 - 설탕 배달 - Swift (0) | 2022.07.10 |
해커랭크(HackerRank) - Almost Sorted // C++ (0) | 2021.05.19 |
댓글