标签:

动态规划

描述:

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. 04-3-object类型

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. JS倒计时两种种实现方式 很不错

    最近做浏览器界面倒计时,用js就实现,两种方式: 一:设置时长,进行倒计时.比如考试时间等等 代码如下: <html> <head> <meta charset=&quo ...

  3. 2018-8-10-WPF-鼠标移动到列表上-显示列表图标

    title author date CreateTime categories WPF 鼠标移动到列表上 显示列表图标 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...

  4. 高德地图定位不到 报错 location Error, ErrCode:7, errInfo:KEY错误 请到http://lbs.amap.com/api/android-location-sdk/abouterrorcode/查看错误码说明.

    出现该问题的可能是高德地图的配置不准确: 仔细配对一下 看sha1 是否是通过应用签名生成的  要区分发布版的sha1 跟调试版的sha1  是不相同的 (小编我第一次反这种错误的时候 是因为我把高得 ...

  5. Ubuntu时间管理方法

    1. date 命令主要用于显示以及修改系统时间 2. hwclock 命令用于查看设置硬件时间,以及同步硬件时间与系统时间 # 显示硬件时间hwclock # 设置硬件时间hwclock -set ...

  6. [转]C#委托的异步调用

    本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: ); //模拟该方法运 ...

  7. 历届试题_DNA比对

    脱氧核糖核酸即常说的DNA,是一类带有遗传信息的生物大分子.它由4种主要的脱氧核苷酸(dAMP.dGMP.dCMT和dTMP)通过磷酸二酯键连接而成.这4种核苷酸可以分别记为:A.G.C.T.     ...

  8. 组件:参数验证props:组件参数验证语法

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  9. java mat复制问题

    意外的发现 org.opencv.core.Mat projectionMat = mat ;//曲线救国,获取同样一个mat projectionMat.setTo());//然后再把颜色换成白色 ...

  10. Delphi代码规范

    1. 前言 本文档主要是为Delphi开发人员提供一个源代码书写标准,以及程序和文件的命名标准,使他们在编程时有一致格式可遵循.这样,每个编程人员编写的代码能够被其他人理解. 2. 源程序书写规范 2 ...