标签:

动态规划

描述:

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.

Example

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

解题思路:

这一题昨天真正纠结了一整天:最后看答案还是没有能够完全理解,幸亏飞飞指点迷津,现在多少有点儿眉目了。

对于,动态规划的问题其实不需要将所有的问题都展开成为二维的矩阵,一维的数组有时可以更好的解决问题,例如这一题,其实需要记录的就是达到target这一状态下需要可以有多少种可能,并不需要记录每个数字存在与否时的可能性。这样,需要维护的变量就会少很多。

另外,不需要强行去照搬套路,有的时候后续子问题和先前子问题不一定在任何情况下都存在关系,按照昨天讨论的结果,是0,他就是0,不要强行在dp[i]上找出与先前的关系。

关于DP是一种思想,最重要的是先前子问题与当前问题在逻辑上某种联系。个人理解,先前子问题可能是一个庞大而繁杂的动态规划问题,但是对于解决当前问题的时候先前问题任何庞大而繁杂的逻辑都体现为子问题最优解的一个节点,并与当前存在某种联系。这种联系称为状态转移方程。

对于本题:

1.子问题划分:

对于这一题只需要在target一个向量上进行划分,因为在每一个target上存在几个数字的可能性并不需要逐一记录,并且这些结果对于后续子问题并不存在实际意义

2.状态转移方程:

如果当前target的值大于nums中的某个数时,当前target上所有的存在解的数量应加上当前target-nums[j]位置上解的数量,因为结果集中加入nums[j]的时候,先前未加入时状态下的解全部需要叠加到当前位置上。

3.初始状态:

开始target为0时任何元素也不加入,存在一个空解,有dp[0]=1.

4.参考代码:

public int backPackVI(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] =1;
for(int i = 1; i<=target; i++){
for(int j =0; j<nums.length; j++){
if(i>=nums[j]){
dp[i] += dp[i-nums[j]];
}
}
}
return dp[target];
}

LintCode刷题笔记-- BackpackIV的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  4. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  5. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  6. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  7. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  8. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

  9. LintCode刷题笔记-- Update Bits

    标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...

随机推荐

  1. 《DSP using MATLAB》Problem 8.22

    时光飞逝,亲朋会一个一个离我们远去,孤独漂泊一阵子后,我们自己也要离开, 代码: %% -------------------------------------------------------- ...

  2. C#生成指定范围内的不重复随机数

    C#生成指定范围内的不重复随机数 // Number随机数个数 // minNum随机数下限 // maxNum随机数上限 public int[] GetRandomArray(int Number ...

  3. mybatis-环境配置-基本案例-和hibernate区别

    Mybatis第一天 1.  Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了goo ...

  4. istringstream字符串流对象

    1.读取字符串流对象 istringstream类用于执行C++风格的字符串流的输入操作. ostringstream类用于执行C++风格的字符串流的输出操作. strstream类同时可以支持C++ ...

  5. webpack 处理图片文件

    1. 安装 file-loader html-loader npm install file-loader html-loader --save-dev 其中html-loader生效需配合 html ...

  6. qq音乐网站页面切换歌手分类时不刷新

    1.提交表单时会自动刷新页面(提交表单一般使用post方式提交) 2.动态加载数据时页面不会刷新,只是把页面中某个位置的内容替换掉想要的内容 3.一般在切换到不同的html页面时才会强制让你把页面刷新 ...

  7. thinkPHP使用中踩的坑,记录一下(不停更)

    版本3.2.3 1.数据库操作中的连贯操作table(),在查询的时候可以切换表,但是在插入,更新的时候请不要使用.例如 D('user')->table('auth')->add($da ...

  8. 19.10.14-Q

    小$P$的咕事 总结: 还行,就是$T1$写的慢了,$T2,T3$暴力有点锅 T1 小模拟. 打就是了. 可以小小的手玩一下. (考试的时候某同志人肉对拍了$20min$)=.= 418 ms 360 ...

  9. 2019牛客暑期多校赛(第一场) A Equivalent Prefixes(单调栈)

    传送门:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个数组a和b,求最大的p,满足在区间 [1,p] 中任何区间的两个数组的最小值的下标都相等. 思 ...

  10. CentOS 6.5 使用Apache的VirtualHost映射SVN端口

    vi /etc/httpd/conf.d/subversion #修改Apache的SVN配置,如果没有配置过则略过 #<Location /svn> # DAV svn # SVNPar ...