Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1931๋ฒˆ - ํšŒ์˜์‹ค ๋ฐฐ์ • (์‹ค๋ฒ„1) (feat. ๊ทธ๋ฆฌ๋””, ํƒ์š•๋ฒ•)

๊ฐ์ž ๐Ÿฅ” 2022. 8. 31. 19:58
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

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

 

1931๋ฒˆ: ํšŒ์˜์‹ค ๋ฐฐ์ •

(1,4), (5,7), (8,11), (12,14) ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์•„์ดํŒจ๋“œ๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด์„œ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ, ์ฃผ์–ด์ง„ ์ž…๋ ฅ๊ฐ’์„ ์ž˜ ์ •๋ ฌํ•œ๋‹ค๋ฉด, ์ˆœ์„œ๋Œ€๋กœ ๋”ํ•ด๊ฐ€๋ฉด์„œ ์ •๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

์šฐ์„  ๋งŽ์€ ํšŒ์˜๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋™์ผํ•œ ์‹œ๊ฐ„์— ์‹œ์ž‘ํ•˜๋Š” ํšŒ์˜๋Š” ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ๋๋‚˜์•ผํ•œ๋‹ค. ์ด์œ ๋Š” ๋นจ๋ฆฌ ๋๋‚ ์ˆ˜๋ก ๋’ค์—์„œ ๊ณ ๋ คํ•ด๋ณผ ํšŒ์˜๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์‹œ์ž‘์‹œ๊ฐ„์„ ์ˆœ์„œ๋กœ ๋จผ์ € ์ •๋ ฌํ•ด์ค€๋’ค, ์ข…๋ฃŒ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ •๋ ฌ์‹œ์ผœ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

์ •๋ ฌ๋œ ํ›„์— prevEndTime์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ๋„ฃ์–ด์ฃผ๊ณ , ๊ฐ ํšŒ์˜๋งˆ๋‹ค start ์‹œ๊ฐ„์„ ๋น„๊ตํ•ด์ค€๋‹ค. ์‹œ์ž‘์‹œ๊ฐ„์ด prevEndTime๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด cnt ๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ณ , prevEndTime์„ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

๐ŸŸ  ์ •๋‹ต ์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1931_%ED%9A%8C%EC%9D%98%EC%8B%A4%20%EB%B0%B0%EC%A0%95/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

import Foundation

// input
let n = Int(readLine()!)!
var meeting = [[Int]]()
for _ in 0..<n {
    let temp = readLine()!.split(separator: " ").map { Int(String($0))! }
    meeting.append(temp)
}

// main
func solution(_ meeting: [[Int]]) -> Int {
    // meeting ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
    var sortedMeetings = meeting
    sortedMeetings.sort{ return $0[0] < $1[0] }
    sortedMeetings.sort{ return $0[1] < $1[1] }

    var cnt = 0
    var prevEndTime = 0

    for meeting in sortedMeetings {
        let start = meeting[0]
        let end = meeting[1]

        if start >= prevEndTime {
            cnt += 1
            prevEndTime = end
        }
    }
    return cnt
}

print(solution(meeting))

 

 

 

๋ฐ˜์‘ํ˜•