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 ...
随机推荐
- PAT甲级——【牛客练习A1004】
题目描述 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For ex ...
- PAT甲级——【牛客练习题1002】
题目描述 Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chi ...
- CentOS 编译golang
CentOS 安装Mercurial http://hi.baidu.com/lang2858/item/cda8f6026cd522e0f45ba67f 获取代码 $ hg clone -u rel ...
- Python学习day07 - Python进阶(1) 内置方法
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 关于mybatis对实体类参数绑定参数的问题
dao层的代码: public interface SupplierMapper extends BaseMapper<SupplierDbo>{ /*List<SupplierDb ...
- C# 把十六进制表示的ASCII码转换为对应的字符组成的字符串
0x30表示字符‘0’的ASCII码.
- java笔试之字符串反转
写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串. package test; import java.util.Scanner; public class exam04 { public ...
- 洛谷 P2886 [USACO07NOV]牛继电器Cow Relays
题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #includ ...
- light oj 1068 数位dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- Spring注解驱动开发(五)-----扩展原理
扩展原理 1.BeanPostProcessor-----bean后置处理器,bean创建对象初始化前后进行拦截工作的 2.BeanFactoryPostProcessor-----beanFacto ...