问题描述

http://www.lintcode.com/zh-cn/problem/backpack-vi/

Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

注意事项

The different sequences are counted as different combinations.

样例
Given nums = [1, 2, 4], target = 4

The possible combination ways are:
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
[2, 2]
[4]
return 6

笔记

在规模较小的时候使用回溯法可以输出所有可能的结果,如代码1所示,但是规模变大就不行了,还是要用动态规划。

动态规划:如代码2所示。
假设buff[i]的意思是,容量为i的时候,有多少种装的方法。

每次考虑把当前num[j]的空间腾出来,然后装进去num[j]。这样的话需要考虑buff[i-num[j]]有多少种方法,把这些方法加到当前的buff[i]中去。

初始化buff[0]=1,因为,假如buff[2]=0,第一件物品num[0] = 2,那么根据上面所说,buff[2]+=buff[0],现在buff[2]应该为2,因为把第一件物品2放进去空包里也算一种方法。

代码1

//
//  main.cpp
//  test
//
//  Created by SteveWong on 8/5/16.
//  Copyright © 2016 SteveWong. All rights reserved.
//#include <iostream>
#include <vector>
using namespace std;class Solution {
public:/*** @param nums an integer array and all positive numbers, no duplicates* @param target an integer* @return an integer*/int backPackVI(vector<int>& nums, int target) {// Write your code hereint cnt = 0;vector<int> tmp;getOne(nums, target, cnt, tmp);return cnt;}void getOne(vector<int> &nums, int target, int &cnt, vector<int> &tmp){if (target == 0){cnt++;for (auto i : tmp){cout << i << ' ';}cout << endl;return;}for (auto i : nums){if (target >= i){tmp.push_back(i);getOne(nums, target-i, cnt, tmp);tmp.pop_back();}}}
};int main(int argc, const char * argv[]) {// insert code here...std::cout << "Hello, World!\n";vector<int> nums = {1,2,4};int target = 4;Solution sol;sol.backPackVI(nums, target);return 0;
}

代码2

class Solution {
public:/*** @param nums an integer array and all positive numbers, no duplicates* @param target an integer* @return an integer*/int backPackVI(vector<int>& nums, int target) {// Write your code hereconst int nItem = nums.size();vector<int> buff(target+1, 0);buff[0] = 1;for (int i = 1; i <= target; i++){for (int j = 0; j < nItem; j++){if (i >= nums[j])buff[i] += buff[i-nums[j]];}}return buff[target];}
};

Backpack VI相关推荐

  1. 九章算法 | Facebook 面试题 : Backpack VI 背包算法

    2017-12-21 题目描述 给一个nums[]数组,如[1, 2, 4] 将这些数组合使得: 这些数的和是给出的一个target,如使这些数的和等于4,求这样的组合有多少个? 样例 样例 [1, ...

  2. 九章算法高频算法题 题解

    专栏 | 九章算法 网址 | http://www.jiuzhang.com Google 面试题: Google 面试题 | 目标和 Google 面试题 | 建邮局 Google 面试题 | 0与 ...

  3. Backpack II 0-1背包

    Given n items with size Ai and value Vi, and a backpack with size m. What's the maximum value can yo ...

  4. 2022“杭电杯”中国大学生算法设计超级联赛(1)1003 Backpack个人题解

    Backpack 题目描述 有n件物品,他们各自都有体积vi和价值wi,给你一个体积为m的背包,求是否能从这n件物品中取出若干件,使得它们的体积之和恰好为m,所有物品的异或和最大,最大值是多少 分析 ...

  5. #125 Backpack II

    题目描述: Given n items with size Ai and value Vi, and a backpack with size m. What's the maximum value ...

  6. lintcode backpack

    Given n items with size A[i], an integer m denotes the size of a backpack. How full you can fill thi ...

  7. LeetCode BackPack 背包问题

    //从上个月末之后就开始有点浮躁起来,本来计划国庆查漏补缺的,结果网卡光荣的坏了ORZ--无论如何,OFFER现在还没有拿到,即便是拿到了也不该放松学习的状态.Stay Hungry,Stay Foo ...

  8. linux vi编辑器中的复制粘贴快捷键

    在使用vi有时会想直接复制一行数据,然后粘贴若干行进行修改 复制一行数据的方法 把光标放到要复制的一行前面,然后按两下yy字母键 yy # 复制光标所在的那一行 然后把光标放到要复制到的地方去,按键盘 ...

  9. Linux 运维常用命令 find、awk、sed、grep、vi、ps、lsof、rpm

    先用一个脚本,模拟创建 14 个测试文件: #!/bin/bash for ((i=1;i<=10;i++)); do if [ $i -lt 3 ] thentouch /home/mysql ...

最新文章

  1. sqlserver查询自定义的函数
  2. 使用签名保护基于HTTP的API
  3. 互联网晚报 | 8月21日 星期六 | 中国电信正式在A股上市;呷哺呷哺将关闭200家亏损门店;个人信息保护法表决通过...
  4. Leecode刷题热题HOT100导航目录
  5. nginx - 性能优化
  6. java udp丢包重发_UDPDataPacker_Java数据报之失序和丢包
  7. 高盛:Github开源、Marquee、SecDB
  8. 《海量数据库解决方案》读后感
  9. Linux C/C++UDP通信实现
  10. Instagram的百万美元Bug
  11. cad计算机画图标准,CAD画图某些常用尺寸及作图习惯
  12. 什么是差模干扰和共模干扰?
  13. 360扫描出来html木马,你的电脑真的做好防护了吗?使用360安全卫士木马查杀一键扫描就知道...
  14. solarflare低延迟网卡_Solarflare低延迟万兆网卡配置和性能调优实践
  15. 循环冗余校验-CRC
  16. android4.4 获取符合默认短信应用要求的短信应用包名(改进篇)
  17. YARN | 让你彻底搞明白YARN资源分配
  18. Unity程序框架总结归置系列(4)——mono模块
  19. SQL server身份验证
  20. 视觉SLAM十四讲 第1-2讲 初识SLAM

热门文章

  1. 不用无线网卡教你如何用台式机连接WiFi
  2. 开发 mirai QQ机器人起步教程
  3. 最好看的Excel条形图 如何用Excel图表把它做出来
  4. 第三届“网鼎杯”官方资格赛圆满结束,问鼎之战即将开启!
  5. c++游戏之城市守卫战
  6. 如何提取公众号文章中的音频
  7. b B KB MB GB TB 之间的关系与换算
  8. 使用opencv将16位深度图转灰度图
  9. 西南大学网络与继续教育学院课程考试试题卷
  10. Python:datetime时间UTC时间转东八区