Skip to content

[Algorithm] TieRopesย #151

@hwangJi-dev

Description

@hwangJi-dev

๐Ÿ’ฌย ๋ฌธ์ œ

https://app.codility.com/programmers/lessons/16-greedy_algorithms/tie_ropes/


๐Ÿ’ฌย Idea

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๊ฐ„ํ•ฉ์„ ๊ตฌํ•œ ๋’ค ํ•ฉ์ด K ์ด์ƒ์ด ๋˜๋ฉด ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ์ด๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.


๐Ÿ’ฌย ํ’€์ด

public func solution(_ K : Int, _ A : inout [Int]) -> Int {
    var sum = 0
    var cnt = 0
    
    for i in A {
        sum += i
        
        if sum >= K {
            cnt += 1
            sum = 0
        }
    }
    
    return cnt
}

์†Œ์š”์‹œ๊ฐ„ : 12๋ถ„

์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N)

ํ‰๊ฐ€ํ‘œ : https://app.codility.com/demo/results/training6874HK-B65/

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions