Skip to content

Instantly share code, notes, and snippets.

@cc2011
Created September 5, 2017 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cc2011/f454765c93e3b86f02125fa7f9f363d7 to your computer and use it in GitHub Desktop.
Save cc2011/f454765c93e3b86f02125fa7f9f363d7 to your computer and use it in GitHub Desktop.
4-keys-keyboard
https://discuss.leetcode.com/topic/97628/java-4-lines-recursion-with-step-by-step-explanation-to-derive-dp
public int maxA(int n) {
int[] dp = new int[n + 1];
for (int i = 0; i <= n; i++) {
dp[i] = i;
for (int j = 1;j <= i - 3; j++) {//dp[j] => with j operations, max num of A we can produce
dp[i] = Math.max(dp[i], dp[j] * (i - j - 1));
dp[j] * (i-j-2) + dp[j]
}
}
return dp[n];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment