2020-08-24

518. 零钱兑换 II

给定不同面额的硬币和一个总金额。写出函数来计算可以凑成总金额的硬币组合数。假设每一种面额的硬币有无限个。
示例 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.

剑指 Offer 36. 二叉搜索树与双向链表

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。
为了让您更好地理解问题,以下面的二叉搜索树为例:
我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。
下图展示了上面的二叉搜索树转化成的链表。“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);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个 SQL 语句,用于向借阅表中插入数据。该表包含以下字段:借阅编号、读者编号、书籍编号、借阅日期、归还日期、借阅状态。每条数据表示一次借阅记录。其中借阅编号、读者编号、书籍编号、借阅日期和借阅状态是必填项,归还日期为可选项,如果借阅状态为“已还”则必须填写归还日期。 具体插入的数据如下: - 借阅编号:100001,读者编号:123413,书籍编号:0001,借阅日期:2020-11-05,归还日期:NULL,借阅状态:借阅 - 借阅编号:100002,读者编号:223411,书籍编号:0002,借阅日期:2020-9-28,归还日期:2020-10-13,借阅状态:已还 - 借阅编号:100003,读者编号:321123,书籍编号:1001,借阅日期:2020-7-01,归还日期:NULL,借阅状态:过期 - 借阅编号:100004,读者编号:321124,书籍编号:2001,借阅日期:2020-10-09,归还日期:2020-10-14,借阅状态:已还 - 借阅编号:100005,读者编号:321124,书籍编号:0001,借阅日期:2020-10-15,归还日期:NULL,借阅状态:借阅 - 借阅编号:100006,读者编号:223411,书籍编号:2001,借阅日期:2020-10-16,归还日期:NULL,借阅状态:借阅 - 借阅编号:100007,读者编号:411111,书籍编号:1002,借阅日期:2020-9-01,归还日期:2020-9-24,借阅状态:已还 - 借阅编号:100008,读者编号:411111,书籍编号:0001,借阅日期:2020-9-25,归还日期:NULL,借阅状态:借阅 - 借阅编号:100009,读者编号:411111,书籍编号:1001,借阅日期:2020-10-08,归还日期:NULL,借阅状态:借阅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值