Combination Sum IV -- LeetCode
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 = [, , ]
target = The possible combination ways are:
(, , , )
(, , )
(, , )
(, )
(, , )
(, )
(, ) Note that different sequences are counted as different combinations. Therefore the output is .
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?
思路:递归求解。
为了避免相同的解重复计数,要将原数组中的重复数字剔除,这样子所有的情况都只会枚举一遍。
同时,为了提速,在递归过程中,可以用一个map记录子问题的结果,这样就可以节省时间。
补充:如果数组中有负数,则应该添加的额外条件是最多可以有几个数相加。
class Solution {
public:
int help(vector<int>& nums, int target, unordered_map<int, int>& solutionCount) {
int count = ;
for (int i = ; i < nums.size() && nums[i] <= target; i++) {
if (nums[i] < target) {
int balance = target - nums[i];
if (solutionCount.count(balance))
count += solutionCount[balance];
else {
int subCount = help(nums, balance, solutionCount);
solutionCount.insert(make_pair(balance, subCount));
count += subCount;
}
}
else count++;
}
return count;
}
int combinationSum4(vector<int>& nums, int target) {
if (nums.size() == ) return ;
sort(nums.begin(), nums.end(), less<int>());
vector<int> distinctNum(, nums[]);
unordered_map<int, int> solutionCount;
for (int i = ; i < nums.size(); i++)
if (nums[i] != nums[i-]) distinctNum.push_back(nums[i]);
return help(distinctNum, target, solutionCount);
}
};
Combination Sum IV -- LeetCode的更多相关文章
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 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 ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Combination Sum II leetcode java
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- Combination Sum III - LeetCode
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 插头dp题表
bzoj1814: Ural 1519 Formula 1 bzoj3125: CITY bzoj1210: [HNOI2004]邮递员 bzoj2331: [SCOI2011]地板 bzoj1187 ...
- [Noip2004]虫食算 dfs
搜索问题的关键:优秀的搜索策略以及行之有效的减枝 对于这道题我们阶乘搜肯定不行所以我们按位搜,我们对每一位的三个数进行赋值,然后判解. 对于此一类的搜索乘上一个几十的常数来减枝往往要比直接搜要快得多, ...
- django 连接 oracle 问题
安装 oracle 后,在 django 项目中连接出现问题记录. 问题1:pip install cx_Oacle 未出现任何问题,但运行过程出现: 原因:连接 oracle 的工具 cx_Orac ...
- Python基础(5)_文件操作
一.文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件打开模式 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文 ...
- Spring - IoC(1): Spring 容器
BeanFactory & ApplicationContext org.springframework.beans.factory.BeanFactory 是最基本的 Spring 容器接口 ...
- openlayers3中应用proj4js
要在openlayers3中应用proj4js,需要在html中引用proj4js,然后在引用所需要的projection的js定义,如 http://epsg.io/21781-1753.js 然后 ...
- 【hdu2222-Keywords Search】AC自动机基础裸题
http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...
- [bzoj3223]文艺平衡树——splay
题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...
- autoKeras Windows 的入门测试
在测试中分析一下ide的效果,在pycharm中测试的时候老师提示内存溢出,而且跑autoKeras的cnn时确实消耗很大空间.但是同样的电脑,换了vscode进行测试的时候没有问题.我也不知道什么回 ...
- bzoj 1500 修改区间 splay
内个我也不知道哪儿不对,TLE了,说说思路吧 其实思路也没什么说的,就是裸的splay,对于最后一个操作 我们记下每个区间的最长前缀,最长后缀,那么最长子序列就是 前缀,后缀,左子树的后缀+右子树的前 ...