给定不同面额的硬币和一个总金额。写出函数来计算可以凑成总金额的硬币组合数。假设每一种面额的硬币有无限个。
示例 1:
输入: amount = 5, coins = [1, 2, 5]
输出: 4
解释: 有四种方式可以凑成总金额:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
示例 2:
输入: amount = 3, coins = [2]
输出: 0
解释: 只用面额2的硬币不能凑成总金额3。
示例 3:
输入: amount = 10, coins = [10]
输出: 1
dp[i][j] : the number of combinations to make up amount j by using the first i types of coins
通过使用前i种硬币来组成金额j所需要的组合数
not using the ith coin, only using the first i-1 coins to make up amount j, then we have dp[i-1][j] ways.
using the ith coin, since we can use unlimited same coin, we need to know how many ways to make up amount j - coins[i-1] by using first i coins(including ith), which is dp[i][j-coins[i-1]]
Initialization: dp[i][0] = 1
Once you figure out all these, it’s easy to write out the code:
public int change(int amount, int[] coins) {
int[][] dp = new int[coins.length + 1][amount + 1];
dp[0][0] = 1;
for (int i = 1; i <= coins.length; i++) {
dp[i][0] = 1;
for (int j = 1; j <= amount; j++) {
dp[i][j] = dp[i-1][j] + (j >= coins[i-1]) ? dp[i][j-coins[i-1]] : 0);
}
}
return dp[coins.length][amount];
}
Now we can see that dp[i][j] only rely on dp[i-1][j] and dp[i][j-coins[i]],
then we can optimize the space by only using one-dimension array.
public int change(int amount, int[] coins) {
int[] dp = new int[amount + 1];
dp[0] = 1;
for (int coin: coins) {
for (int i = coin; i <= amount; i++) {
dp[i] = dp[i] + dp[i-coin];
}
}
return dp[amount];
}
/*
increasing i then the previous partial result dp[i - coin] is the result that has considered coin already
decreasing i then the previous partial result dp[i - coin] is the result that has not considered coin yet
*/
/**
* @return number of ways to make sum s using repeated coins
*/
public static in coinrep(int[] coins, int s) {
int[] dp = new int[s + 1];
dp[0] = 1;
for (int coin: coins) {
for (int i = coin; i <= s; i++) {
dp[i] += dp[i - coin];
}
}
return dp[s];
}
public static int coinnonrep(int[] coins, int s) {
int[] dp = new int[s + 1];
dp[0] = 1;
for (int coin: coins) {
for (int i = s; i >= coin; i--) {
dp[i] += dp[i-coin];
}
}
return dp[s];
}
I took a while to think why dp[i][j] = dp[i-1][j] + (j >= coins[i-1] ? dp[i][j-coins[i-1]] : 0); Here’s my thought:
dp[i - 1][j]: means the number of combinations if we compeletly don’t use the ith coin
dp[i][j - coins[i - 1]]: we must use at least one of the ith coin, so we expell the ith coin from j (Exclusive, opposite to the upper condition)
dp[i - 1][j]: 完全不用当前硬币组成j有多少种组合
dp[i][j - coins[i - 1]] :使用至少一个当前硬币(与上面一条是互斥事件)组成j有多少组合
dp[i][0] is initialized as 1 is because that:
dp[i][0] is actually used and only used in dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]]. When the amount of j equals coins[i-1], we will get a dp[i][0]. So dp[i][0] actually means how many ways to use at least one i-th coin to make up of the amount of coins[i-1] (which is the i-th coin). There is only one way: use only one i-th coin itself. So it should be initiated as 1.
I think dp[i][0] means how many ways we have to use first i coins to make up the amount of 0 , so it’s clear that we can have 1 way that we use none of them.
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。
为了让您更好地理解问题,以下面的二叉搜索树为例:
我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。
下图展示了上面的二叉搜索树转化成的链表。“head” 表示指向链表中有最小元素的节点。
class Solution {
Node pre, head;
public Node treeToDoublyList(Node root) {
if(root == null) return null;
dfs(root);
head.left = pre;
pre.right = head;
return head;
}
void dfs(Node cur) {
if(cur == null) return;
dfs(cur.left);
if(pre != null) pre.right = cur;
else head = cur;
cur.left = pre;
pre = cur;
dfs(cur.right);
}
}