650.2 Keys Keyboard Posted on 2017-09-13 | In LeetCode Solution 1: accepted 28msDP. Base case: A[1] = 0 Induction rule: A[n] if n%2 == 0, A[n] = dp[n] + 2 if n%2 != 0, A[n] = max(dp[j] + n/j), where n%j = 0 1234567891011121314151617181920212223class Solution { public int minSteps(int n) { if (n < 1) { return 0; } int[] dp = new int[n + 1]; for (int i = 2; i <= n; i++) { if (i % 2 == 0) { dp[i] = dp[i / 2] + 2; } else { for (int j = 3; j < i / 2; j++) { if (i % j == 0 && dp[j] != 0) { dp[i] = dp[j] + (i / j); } } if (dp[i] == 0) { dp[i] = i; } } } return dp[n]; }}