๋ฌธ์ ๐ฝ
https://school.programmers.co.kr/learn/courses/30/lessons/12985
๋ค๋ฅธ์ฌ๋ ํ์ด ๐ฝ
function solution(n,a,b)
{
let answer = 0;
while(a !== b) {
a = Math.ceil(a/2);
b = Math.ceil(b/2);
answer++;
}
return answer;
}
๋ดํ์ด๐ฝ
1. ์คํจ โ => ๋ ผ๋ฆฌ ์ค๋ฅ
function solution(n,a,b)
{
var answer = 0;
let count = Math.log(n)/Math.log(2)
let personCount = n
while(1){
if((personCount/2+0.5-a)*(personCount/2+0.5-b)<0){
answer = count;
break;
}else{
personCount=personCount/2
answer = --count;
}
}
return answer;
}
2. ์ฑ๊ณต โญ
function solution(n,a,b)
{
var answer = 0;
let round=1;
while(1){
if(Math.abs(a-b)===1&&Math.min(a,b)%2===1){
answer = round
break;
}
a=Math.ceil(a/2)
b=Math.ceil(b/2)
++round;
}
return answer;
}
๋๋์ ๐ฝ
์๊ณ ๋ฆฌ์ฆ์ ์งํ ํ ๋ count๋ฅผ ์์์ ๋ถํฐ ์ฌ๋ ค๊ณ ํ๋๋ฐ ์ด๋ฐ ๋ฐฉ๋ฒ์ผ๋ก๋ ํ์ํ๊ธฐ๊ฐ ์ด๋ ค์ ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ๋ค. ์ผ๋จ ํด๋น๋ผ์ด๋์์ ๋ง๋ ์กฐ๊ฑด์ ๊ตฌํ๋ค ์๋ ์ ๋ผ์ด๋๋ฅผ ์ฌ๋ฆฌ๋ฉด์ counting ํ๋ค. ์ด๋ ๊ฒ ์งํํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ ์ ์์๋ค.