SW Test/Programmers
[Programmers] Lv2. μμλμ§ν(kotlin)
An effort will never betray π
2022. 9. 20. 01:13
λ°μν
λ¬Έμ
μμ
νμ΄
- (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
}
}
λ°μν