Skip to content

[Algorithm] CountDivΒ #139

@hwangJi-dev

Description

@hwangJi-dev

πŸ’¬Β λ¬Έμ œ

https://app.codility.com/programmers/lessons/5-prefix_sums/count_div/


πŸ’¬Β Idea

  • Aκ°€ K둜 λ‚˜λˆ„μ–΄λ–¨μ–΄μ§€μ§€ μ•ŠλŠ”λ‹€λ©΄ Bμ—μ„œ Kλ₯Ό λ‚˜λˆˆ κ°’μ—μ„œ Aλ₯Ό K둜 λ‚˜λˆˆ 값을 λΉΌμ„œ λ¦¬ν„΄ν•˜κ³ , Aκ°€ K둜 λ‚˜λˆ„μ–΄λ–¨μ–΄μ§„λ‹€λ©΄ Bμ—μ„œ Kλ₯Ό λ‚˜λˆˆ κ°’μ—μ„œ Aλ₯Ό K둜 λ‚˜λˆˆ 값에 1을 λ”ν•΄μ„œ λ¦¬ν„΄ν•œλ‹€.

πŸ’¬Β ν’€μ΄

public func solution(A : Int, B : Int, K : Int) -> Int {
    if A % K != 0 {
        return (B / K) - (A / K)
    } else {
        return (B / K) - (A / K) + 1
    }
}

μ†Œμš”μ‹œκ°„ : 15λΆ„

μ‹œκ°„ λ³΅μž‘λ„ : O(1)

ν‰κ°€ν‘œ : https://app.codility.com/demo/results/trainingMDRS56-ZFR/

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions