Algorithm

λ°±μ€€ - νšŒμ˜μ‹€ λ°°μ •(Swift)

CheonD 2022. 10. 23. 02:07

λ°±μ€€ - νšŒμ˜μ‹€ λ°°μ •(Swift)

 

 

 λ¬Έμ œ μ„€λͺ…

 

https://www.acmicpc.net/problem/1931

 

1931번: νšŒμ˜μ‹€ λ°°μ •

(1,4), (5,7), (8,11), (12,14) λ₯Ό μ΄μš©ν•  수 μžˆλ‹€.

www.acmicpc.net

 

 λ‚˜μ˜ 풀이

 

κ°€μž₯ λ§Žμ€ 회의λ₯Ό ν•˜κΈ° μœ„ν•΄μ„œλŠ” νšŒμ˜μ‹œκ°„μ΄ λΉ¨λ¦¬λλ‚˜λŠ”λŒ€λ‘œ ν•΄μ•Όν•œλ‹€.

 

λ§Œμ•½ νšŒμ˜κ°€ 일찍 μ‹œμž‘ν•˜λŠ” κ²ƒλΆ€ν„°ν•œλ‹€λ©΄ νšŒμ˜μ‹œκ°„μ΄ κΈΈκΈ°λ•Œλ¬Έμ— 졜적이 μ•„λ‹ˆκ³  νšŒμ˜μ‹œκ°„μ΄ 짧은 μœ„μ£Όλ‘œ ν•œλ‹€λ©΄ μ§§μ€κ²Œ 뒀에 μ‘΄μž¬ν•œλ‹€λ©΄ 이것 μ—­μ‹œ λ°˜λ‘€κ°€ μ‘΄μž¬ν•˜κ²Œ λœλ‹€.

 

import Foundation

let n = Int(readLine()!)!

var tup: [(Int, Int)] = []
var currentEnd = -1
var cnt = 0
for i in 0..<n {
    let input = readLine()!.components(separatedBy: " ").map{ Int($0)! }
    tup.append((input[0], input[1]))
}

tup.sort { $0.1 == $1.1 ? $0.0 < $1.0 : $0.1 < $1.1}

for x in tup {
    if x.0 >= currentEnd {
        currentEnd = x.1
        cnt += 1
    }
}

print(cnt)