๋ฐฑ์ค€ - ํšŒ์˜์‹ค ๋ฐฐ์ •(Swift)

2022. 10. 23. 02:07ใ†Algorithm

๋ฐฑ์ค€ - ํšŒ์˜์‹ค ๋ฐฐ์ •(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)