const isQuotientEqual = (a, b, divisor) => {
return Math.floor(a / divisor) == Math.floor(b / divisor);
};
const isRemainderInRange = (a, b, divisor) => {
return Math.abs((a % divisor) - (b % divisor)) < divisor;
};
const solution = (n, a, b) => {
let round = 1;
a--;
b--;
while (true) {
const divisor = 2 ** round;
if (isQuotientEqual(a, b, divisor) && isRemainderInRange(a, b, divisor)) {
return round;
}
round++;
}
};
클린코드의 관점에서 조건문은 어디까지 캡슐화해야 좋은 걸까
isQuotientEqual && isRemainderInRange도 하나의 함수로 묶어야 하나..?
(isMatched 이런 걸로?)
내가 접근한 방법은 a-1과 b-1를 각각 2의 round승으로 나눈 몫이 같고,
또한 a-1과 b-1를 각각 2의 round승으로 나눈 나머지의 차가 2의 round승보다 작을 때
해당 round에서 만난다는 것이었다.
그런데 다른 사람 풀이는 딱 문제에서 제시한 기준을 따라서 훨씬 직관적이었다.
(다음 라운드에 진출하면서 번호가 바뀐다는 것)
이 방법에 따르면 굳이 a와 b를 인덱스처럼 -1빼주지 않아도 된다.
와 log2메소드를 써서 푸는 사람도 있네;;