Skip to content

[Algorithm] ParityDegreeย #159

@hwangJi-dev

Description

@hwangJi-dev

๐Ÿ’ฌย ๋ฌธ์ œ

https://app.codility.com/programmers/trainings/5/parity_degree/


๐Ÿ’ฌย Idea

  • 2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Œ์„ ์œ ์˜ํ•˜์ž. 2^0 = 1
  • 0๋ถ€ํ„ฐ N๊นŒ์ง€ ๋Œ๋ฉด์„œ 2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ๊ตฌํ•˜๊ณ , ํ•ด๋‹น ๊ฐ’์ด N๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๊ณ  N์ด ํ•ด๋‹น 2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์œผ๋กœ ๋‚˜๋ˆ ์ง„๋‹ค๋ฉด maxPow๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

๐Ÿ’ฌย ํ’€์ด

public func solution1(N : Int) -> Int {
    var maxPow = 0
    
    for i in 0..<N {
        let p = Int(pow(2.0, Double(i)))
        if N < p { break }
        
        if N % p == 0 {
            maxPow = max(i, maxPow)
        }
    }
    
    return maxPow
}

**์†Œ์š”์‹œ๊ฐ„** :

ํ‰๊ฐ€ํ‘œ : https://app.codility.com/demo/results/trainingU3BJ6B-8FZ/

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions