SW Test/Programmers

[Programmers] Lv2. μ˜ˆμƒλŒ€μ§„ν‘œ(kotlin)

An effort will never betray 😎 2022. 9. 20. 01:13
λ°˜μ‘ν˜•

문제

https://school.programmers.co.kr/learn/courses/30/lessons/12985

 

 

 

μ˜ˆμ‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/12985

 

 

 

풀이

  • (1,2), (3,4), (5,6) ..  μ΄λ ‡κ²Œ 같은 그룹으둜 λ¬Άμ—¬μ•Ό ν•œλ‹€. 
  • 이λ₯Ό μœ„ν•΄μ„œλŠ” λ‹¨μˆœνžˆ 2둜 λ‚˜λˆ„λ©΄ 같은 값이 μ•ˆλ‚˜μ˜€κΈ°μ— 2둜 λ‚˜λˆˆ λͺ«κ³Ό 2둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€λ₯Ό λ”ν•˜λ©΄ 같은 값을 κ°€λ¦¬ν‚€κ²Œ λœλ‹€.
    (1,1), (2,2), (3,3) ...
  • λ”°λΌμ„œ ν•΄λ‹Ή 두 값이 κ°™μœΌλ©΄ μ’…λ£Œμ‹œν‚€λ©΄ λ©λ‹ˆλ‹€.
class Solution {
    fun solution(n: Int, a: Int, b: Int): Int { 
        var answer = 0
        var first = if(a>b) b else a
        var second = if(a>b) a else b
        
        while(second - first > 0){
            first = first/2 + first%2
            second = second/2 + second%2
            answer++
        }
        
        return answer
    }
}

 

λ°˜μ‘ν˜•