We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static int[] minMoves = new int[1000001];
public static int minMove(int n) {
if (n <= 3) return n;
if (minMoves[n] > 0) return minMoves[n];
int min = Integer.MAX_VALUE;
for (int i=2; i<=Math.sqrt(n); i++) {
if (n % i == 0) {
int factor = n/i;
min = Math.min(min, 1 + minMove(factor));
}
}
min = Math.min(min, 1 + minMove(n-1));
minMoves[n] = min;
return min;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Down to Zero II
You are viewing a single comment's thread. Return to all comments →
Simple DP solution in Java