-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/
๐ฌย Idea
- ์ ๋ถ์ ์ค์ฒฉ๋์ง ์์ ์ต๋๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ . ๋ฐฐ์ด์ ์ฒซ end์ ๋ค์ ์ธ๋ฑ์ค์ start๋ฅผ ๋น๊ตํด ์ต๋ ๊ฐ์๋ฅผ ์ฐพ์์ฃผ๋ฉด ๋๋ค.
๐ฌย ํ์ด
public func solution(_ A : inout [Int], _ B : inout [Int]) -> Int {
if A.isEmpty { return 0 }
var end = B[0]
var cnt = 1
for i in 1..<A.count {
if A[i] > end {
cnt += 1
end = B[i]
}
}
return cnt
}
์๊ฐ ๋ณต์ก๋
: O(N)
ํ๊ฐํ
: https://app.codility.com/demo/results/trainingR64NKR-G29/