当前位置:网站首页>LeetCode: 377. Combined sum IV
LeetCode: 377. Combined sum IV
2022-06-24 09:40:00 【Whisper_ yl】
Here you are Different An array of integers nums , And a target integer target . Please start from nums Find and return the sum of target The number of element combinations of .
The data of the questions ensure that the answers are in line with 32 Bit integer range .
Example 1:
Input :nums = [1,2,3], target = 4
Output :7
explain :
All possible combinations are :
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Please note that , Different sequences are considered as different combinations .
Example 2:
Input :nums = [9], target = 3
Output :0
Tips :
1 <= nums.length <= 200
1 <= nums[i] <= 1000
nums All elements in Different from each other
1 <= target <= 1000
analysis :
At first, I used the search method to do , It turned out to be over time . Later I saw that it was necessary to use a dynamic gauge , In fact, it is a very simple problem . for instance , If target = 6,nums = {2, 3, 4}, that dp[6] = dp[4] + dp[3] + dp[2], I.e. composition 6 The combination number of is from 4,3,2 These three numbers are transferred from . The border dp[0] = 1. Just one thing to be aware of is dp[i - nums[j]] < INT_MAX - dp[i], One limitation of taphole .
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1);
dp[0] = 1;
for(int i = 1; i <= target; i++)
for(int j = 0; j < nums.size(); j++)
if(nums[j] <= i && dp[i - nums[j]] < INT_MAX - dp[i])
dp[i] += dp[i - nums[j]];
return dp[target];
}
};
边栏推荐
- Easyexcel single sheet and multi sheet writing
- P6117-[JOI 2019 Final]コイン集め【贪心】
- Weekly recommended short video: talk about "meta universe" with a serious attitude
- Ora-16038 ora-19502 ora-00312 troubleshooting
- R ellipse random point generation and drawing
- latex公式及表格识别
- 数字化转型的失败原因及成功之道
- 2022.06.23 (traversal of lc_144,94145\
- Algorithm -- find and maximum length k subsequence (kotlin)
- Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
猜你喜欢

tp5 使用post接收数组数据时报variable type error: array错误的解决方法

L01_ How is an SQL query executed?

e的lnx为什么等于x

Servlet快速筑基

Learning Tai Chi Maker - esp8226 (XIII) OTA

Zero foundation self-study SQL course | having clause

深入解析 Apache BookKeeper 系列:第三篇——读取原理

【bug】@JsonFormat 使用时出现日期少一天的问题

Oracle数据文件头SCN不一致处理方法

Ggplot2 color setting summary
随机推荐
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
Cdga | how can we do well in data governance?
关于thinkphp5 使用模型save()更新数据提示 method not exist:think\db\Query-&gt; 报错解决方案
文献调研报告
The ambition of JD instant retailing from 618
Software system dependency analysis
20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)
CF566E-Restoring Map【bitset】
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
蜜罐2款hfish,ehoney
MYCAT read / write separation and MySQL master-slave synchronization
【自定义Endpoint 及实现原理】
Epidemic situation, unemployment, 2022, we shouted to lie down!
An open source monitoring data collector that can monitor everything
从618看京东即时零售的野心
Turn to: CEO of Samsung Electronics: all decisions should start from recognizing yourself
R ellipse random point generation and drawing
Seekbar with text: customize progressdrawable/thumb: solve incomplete display
How to locate lock waiting in Dameng database
ggplot2颜色设置总结