This solution is accepted by the online judge system. But I cannot figure out why.

```
/**
* @param {number} n
* @return {number}
*/
var integerReplacement = function(n) {
if (n < 4) return [0, 0, 1, 2][n];
switch (n % 4) {
case 0: case 2: return integerReplacement(n / 2) + 1;
case 1: return integerReplacement((n - 1) / 4) + 3;
case 3: return integerReplacement((n + 1) / 4) + 3;
}
};
```