LintCode刷题笔记-- BackpackIV
标签:
动态规划
描述:
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的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
随机推荐
- spring容器创建bean对象的方式
xml文件中有bean的配置,而且这个bean所对应的java类中存在一个无参构造器 那么这个时候spring容器就可以使用反射调用无参构造器来创建实例了(常规的方式) 通过工厂类获得实例(工厂类实现 ...
- ElasticSearch入门介绍之会当凌绝顶(一)
ElasticSearch也是一款非常优秀的开源的全文检索框架,以大名鼎鼎的Apache Lucene为基础,高度封装了更丰富,易用的API,同时与Apache Solr一样,提供了非常强大的分布式集 ...
- AlexNet详细解读
AlexNet详细解读 目前在自学计算机视觉与深度学习方向的论文,今天给大家带来的是很经典的一篇文章 :<ImageNet Classification with Deep Convolutio ...
- iTerm2配色和去掉profile提示框
效果: 配色方案代码地址: https://github.com/mbadolato/iTerm2-Color-Schemes 点击最右边的绿色区域,再点击 “import”, 打开刚下载解压好的文 ...
- 关于jQuery中attr(),prop()的使用
注意:什么时候使用attr(),什么时候使用prop()?1.添加属性名称该属性就会生效应该使用prop();2.是有true,false两个属性使用prop();3.其他则使用attr(); 以下是 ...
- 提交代码出现 Push to origin/master was rejected 错误解决方法
转至博客:http://www.xtyos.cn/archives/qt-1-index 为什么会出现这样的问题 一般发生在 GitHub 或 码云 刚刚创建仓库第一次pull的时候,两个仓库的差别非 ...
- Python学习之列表--自动超市购物车
效果图: 实现代码: menu = [0,5000,500,9000,3000,30,50,7000,70,40]name = [0,"iphone","bicycle& ...
- Vue.之.回到顶部
Vue.之.回到顶部 当页面出现上下滚动条时,页面右下角出现回到顶部功能. 在页面上添加如下DIV(写的CSS内部样式),这个DIV功能:出现滚动条往下滑动,就显示出来,反之隐藏.点击DIV快速回到顶 ...
- 2019-8-31-C#-程序集数量对软件启动性能的影响
title author date CreateTime categories C# 程序集数量对软件启动性能的影响 lindexi 2019-08-31 16:55:58 +0800 2018-10 ...
- Ubuntu 18.04 美化
Ubuntu 18.04 美化 sudo apt install gnome-tweak-tool sudo apt install gnome-shell-extensions sudo apt i ...