Dynamic Programming
70. Maximum Subarray
TODO
71. 322. Coin Change
class Solution {
public int coinChange(int[] coins, int amount) {
// Unknown: -1; Yes: 1; No: 0
int[] dp = new int[amount+1];
Arrays.fill(dp, -1);
int result = helper(coins, amount, dp);
return result == Integer.MAX_VALUE ? -1 : result;
}
private int helper(int[] coins, int amount, int[] dp) {
if (amount == 0) {
return 0;
}
if (amount < 0) {
return Integer.MAX_VALUE;
}
if (dp[amount] != -1) {
return dp[amount];
}
int steps = Integer.MAX_VALUE;
for (int coin : coins) {
int ans = helper(coins, amount - coin, dp);
if (ans != Integer.MAX_VALUE) {
steps = Math.min(ans + 1, steps);
}
}
dp[amount] = steps;
return dp[amount];
}
}TODO
74. Unique Paths
TODO
recursive
memorized
dp
Last updated
Was this helpful?