4 ms·
It's a particularly good example of memoization: both simple and without a suitable DP solution. int collatz(int n) { return n == 1 ? 0 : 1 + collatz(n
by paulddraper 7y ago
It's a particularly good example of memoization: both simple and without a suitable DP solution.
int collatz(int n) {
return n == 1 ? 0 : 1 + collatz(n % 2 ? 3 * n + 1 : n / 2)
}