377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]
target = 4 The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.
Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?
class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
nums = [1, 2, 3]
target = 4 f(4) = [1,f(3)] if 1 in nums U [2, f(2)] if 2 in nums U [3, f(1)] if 3 in nums U [4, f(0)] if 4 in nums The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
"""
dp = [1] + [0]*target
for x in range(1, target+1):
for n in nums:
if x >= n:
dp[x] += dp[x-n]
return dp[target]
377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果的更多相关文章
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...
随机推荐
- poj 3122 (二分查找)
链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- ZOJ 3819 Average Score(平均分)
Description 题目描述 Bob is a freshman in Marjar University. He is clever and diligent. However, he is n ...
- yii 常用路径
yii::app()->homeurl //主页的网址 yii系统变量. //得到proteced目录的物理路径 Yii::app()->basePath; 调用YII框架中jquery: ...
- eclispse 中集成多个tomcat
1.背景 在本地需要运行两个项目进行测试时,需要同时启动两个服务器,所以集成多个Tomcat到eclipse就成为一个必要的知识点. 2.准备知识 2.1 因为同时在一台主机上运行,所以多个服务器共用 ...
- 子div用了float浮动之后,如何撑开父元素,让父元素div自动适应高度的问题
方法一: html: <div id="all1"> <div id="left1">1</div> <div id= ...
- bootstrap学习笔记<一>(bootstrap用法)
首先引入bootstrap官网链接:http://www.bootcss.com/ bootstrap 3下载地址:http://v3.bootcss.com/getting-started/#dow ...
- Echarts个人实例
1.deviceOperateTrendIndex.jsp <%@ page language="java" contentType="text/html; cha ...
- Android ViewFlipper控件实例
使用ViewFlipper实现两张图片切换效果,废话不多说,直接上代码. java源码: package com.example.viewflipper; import android.os.Bund ...
- 【Todo】单例模式各种实现方式及并发安全
Java 40道面试题不错:http://www.tuicool.com/articles/VRVFZb 其中有一道题目: 单例模式的线程安全性 老生常谈的问题了,首先要说的是单例模式的线程安全意味着 ...
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...