문제 링크
https://www.acmicpc.net/problem/1931
1931번: 회의실 배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
문제 풀이
fun main() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val times = Array(n) { Array(2) { 0 } }
repeat(n) {
val line = readLine().split(" ")
times[it][0] = line[0].toInt()
times[it][1] = line[1].toInt()
}
times.sortWith(compareBy<Array<Int>> { it[1] }.thenBy { it[0] })
var currentTime = times[0][1]
var result = 1
for (i in 1 until times.size) {
if (currentTime <= times[i][0]) {
currentTime = times[i][1]
result += 1
}
}
println(result)
}
2가지의 기준으로 오름차순 정렬을 수행합니다.
1) 회의가 끝나는 시간 2) 회의가 시작되는 시간
그리디하게 앞에서부터 가능한 회의를 잡는 방식으로 구현했습니다.
가능한 회의 시간이 있을 경우
회의가 끝나는 시간을 currentTime 변수에 갱신시키고
회의 개수를 1씩 증가시킵니다.
'Algorithm > BOJ' 카테고리의 다른 글
[백준] 1043 거짓말 (파이썬 python) (0) | 2024.02.10 |
---|---|
[백준] 2559 수열 (코틀린 kotlin) (0) | 2024.02.02 |
[백준] 13458 시험 감독 (파이썬 python) (0) | 2022.03.16 |
[백준] 14890 경사로 (파이썬 python) (0) | 2022.03.13 |
[백준] 14889 스타트와 링크 (파이썬 python) (0) | 2022.03.08 |