322. Coin Change

Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of

money cannot be made up by any combination of the coins, return -1.You may assume that you have an infinite

number of each kind of coin.

解题思路:

比较典型的背包问题。这里f[i]存储金额为i时使用硬币的最小数目,c[i]是硬币的价值,w[i]都为1,表示使用一个这样的硬币。

状态转移方程:f[i] = min(f[i], f[i-coins[j]+1),针对i >= coins[j]。

注意:数组要初始化。。f[i]都设为amount+1(作为一个最大值)。f[0]=0。

int coinChange(vector<int>& coins, int amount) {
if (amount == 0)
return 0;
int f[amount + 1];
int i, j;
// initialization
f[0] = 0;
for (i = 0; i <= amount; i++) {
// initialization
if (i > 0)
f[i] = amount + 1;
for (j = 0; j < coins.size(); j++) {
if (i >= coins[j]) {
if (f[i - coins[j]] + 1 < f[i]) {
f[i] = f[i - coins[j]] + 1;
}
}
}
}
// if amount can not be reached, f[amount] = amount+1
return f[amount] > amount ? -1:f[amount];
}

416. Partition Equal Subset Sum

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

解题思路:

首先,如果所有数目的和是奇数,那么是false。然后我们看一下sum /= 2是否可以得到。状态转移方程:

dp[j] = max(dp[j], dp[j-nums[i] + nums[i])。初始化dp[0] = 0, dp[i] = sum+1。

如果说几个数相加可以得到sum,则另外几个相加也可以得到sum。因此只要看dp[sum]是否为sum即可。

bool canPartition(vector<int>& nums) {
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
sum += nums[i];
}
if (sum % 2 != 0) return false;
sum /= 2;
int dp[sum + 1] = {sum + 1};
dp[0] = 0;
for (int i = 0; i < nums.size(); i++) {
for (int j = sum; j >= nums[i]; j--) {
if (dp[j - nums[i]] + nums[i] > dp[j])
dp[j] = dp[j - nums[i]] + nums[i];
}
}
return dp[sum] == sum;
}

leetcode-21-knapsack的更多相关文章

  1. [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

    21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...

  3. Java实现 LeetCode 21 合并两个有序链表

    21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1 ...

  4. LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)

    题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description   Problem: 已知两个有序链表(链表中的数 ...

  5. LeetCode(21)题解:Merge Two Sorted Lists

    https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...

  6. LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. <每日 1 OJ> -LeetCode 21. 合并两个有序链表

    题目: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4输出:1->1-> ...

  8. [LeetCode] 21. Merge Two Sorted Lists 合并有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. [LeetCode]21. 合并两个有序链表(递归)

    题目 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-> ...

  10. LeetCode 21 -- Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

随机推荐

  1. Http请求数据解释

    请求的数据里面包含三个部分内容 : 请求行 . 请求头 .请求体 请求行 POST /examples/servlets/servlet/RequestParamExample HTTP/1.1 PO ...

  2. beanshell解析json(从简单到复杂)

    使用beanshell 解析单层Json: Json 数据如下: { "status":200, "code": 0, "message": ...

  3. centos虚拟机安装指定版本docker

    环境: centos 7.6+ docker-ce 17.03.2 安装依赖包 yum -y install yum-utils device-mapper-persistent-data lvm2 ...

  4. kali linux安装搜狗输入法

    1. 更新软件源 vi /etc/apt/sources.list 2. 安装fcitx apt-get install fcitx 3. 下载deb包到指定目录, dpkg -i sogou**** ...

  5. java ReentranLock锁

    1.效果和synchronized一样,都可以同步执行,lock方法获得锁,unlock方法释放锁 使用示例: package com.test; import java.util.concurren ...

  6. To the world you may be one person, but to one person you may be the world.

    To the world you may be one person, but to one person you may be the world.对于世界而言,你是一个人:但对于某人而言,你是他的 ...

  7. 零基础逆向工程11_C语言05_结构体

    结构体小结 结构体是按照分配的大小,局部变量会自动数据对齐 1字节对齐,省空间,但cpu查找效率低 4字节对齐,不省空间,但cpu查找效率高 VC6默认的结构对齐大小 项目右键-> settin ...

  8. zabbix2升级zabbix3

    1.停服zabbix2,停服数据库/etc/init.d/zabbix_server stop/etc/init.d/mysqld stop 2.物理备份数据库cd /var/lib/mysql/ta ...

  9. [LoadRunner]录制启动时报“The JVM could not be started……”错误解决方案

    在LR准备点击录制java over http协议时,程序报如下错误: 报错提示是设置的JVM值设置问题,导致不能启动. 解决方案一 点击F4快捷按钮,会弹出以下界面,在选中的位置选择对应的java路 ...

  10. Outlook 2016 自动发送/接收无法正常工作

    如果您的自动/发送接收由于某种原因停止工作,可能会非常令人沮丧,因为您必须记住手动执行发送/接收(F9).如果您遇到Outlook无法自动发送或接收电子邮件的问题,可以尝试以下几项操作. #1 发送/ ...