Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] MaxNonoverlappingSegments #150

Closed
hwangJi-dev opened this issue Mar 24, 2023 · 0 comments
Closed

[Algorithm] MaxNonoverlappingSegments #150

hwangJi-dev opened this issue Mar 24, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Mar 24, 2023

๐Ÿ’ฌย ๋ฌธ์ œ

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant