Given a number N and number K, find minimum number of operations to convert N to 1. allowed operations:
- subtract 1
- divide by 2 (at most K times)
here is my code but it is not working correctly.
private int getMinRounds(int N, int K, int memo[])
{
if(N <= 1) return 0;
if (memo[N] != -1) return memo[N];
int rounds;
if ((N%2) == 0 && K > 0) {
rounds = 1 + Math.min(getMinRounds(N-1, K, memo), getMinRounds(N/2, K-1, memo));
}else {
rounds = 1 + getMinRounds(N-1, K, memo);
}
memo[N] = rounds;
return rounds;
}
private int solution(int N, int K)
{
int memo[] = new int[N + 1];
Arrays.fill(memo, -1);
return getMinRounds(N, K, memo);
}