Leetcode 377. Combination Sum IV
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?
例子:[1,2,3,5,6], target = 8。dp[8] 意为有多少种方式可以生成8。
例如: 8=1(in nums) + 7 = 2(in nums) + 5 = 3(in nums) + 4= 5(in nums) + 3 = 6(in nums) + 2。 所以dp[8] = dp[7] + dp[5] + dp[4] + dp[3] + dp[2]。
5=5(in nums) + 0 = 3(in nums) + 2 = 2(in nums) + 3 = 1(in nums) +4。 所以dp[5] = dp[0] + dp[2] + dp[4] + dp[4]。
class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
dp = [0]*(target + 1)
dp[0] = 1
for i in range(target+1):
for n in nums:
if i + n <= target:
dp[i+n] += dp[i]
return dp[-1]
Leetcode 377. Combination Sum IV的更多相关文章
- [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 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LC 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- leetcode日记 Combination sum IV
题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...
- 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 comb ...
随机推荐
- Android中Listview展示及其优化好处
展示效果: 中间的item条目是可以上下滑动的. 代码实现: @Override public View getView(int position, View convertView, ViewGro ...
- linux数据误删后,灾难性数据备份与数据还原
一 准备工作 #rm –rf 误删重要数据怎么办? 1. 要冷静,通知停止该服务器一切操作 2. 查看被删除文件所在分区 #mount 3. 将该分区设置为只读 #mount -r -n -o re ...
- DP优化与换零钱问题
1 当贪心不再起效的时候 对于换零钱问题,最简单也是性能最好的方法就是贪心算法.可是贪心算法一定要满足面值相邻两个零钱至少为二倍关系的前提条件.例如1,2,5,10,20……这样的零钱组应用贪心最简单 ...
- RMAN还原遭遇ORA-32006&ORA-27102错误
案例环境: 服务器A: 操作系统 : Red Hat Enterprise Linux ES release 4 (Nahant Update 6) 数据库版本: Oracle Database ...
- Linux系统VNC配置实践总结
VNC概述 VNC (Virtual Network Computing)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在 ...
- Linux启动报错missing operating system
用UltraISO制作了一个Red Hat Enterprise Linux Server release 5.7系统的U盘启动盘,然后在一台PC上安装,由于安装过程中在干别的事情,有些选项没有细看. ...
- C语言--乱写C语言
C语言的语法太枯燥了 换个写法 #include <stdio.h> #include<stdlib.h> #define end } #define if(x) if ( ...
- Conquer and Divide经典例子之Strassen算法解决大型矩阵的相乘
在通过汉诺塔问题理解递归的精髓中我讲解了怎么把一个复杂的问题一步步recursively划分了成简单显而易见的小问题.其实这个解决问题的思路就是算法中常用的divide and conquer, 这篇 ...
- 大数据系列(1)——Hadoop集群坏境搭建配置
前言 关于时下最热的技术潮流,无疑大数据是首当其中最热的一个技术点,关于大数据的概念和方法论铺天盖地的到处宣扬,但其实很多公司或者技术人员也不能详细的讲解其真正的含义或者就没找到能被落地实施的可行性方 ...
- 自定义样式RatingBar的使用
1.设置布局文件,自定义ratingbar样式 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...