当前位置:网站首页>LeetCode_动态规划_中等_377.组合总和 Ⅳ
LeetCode_动态规划_中等_377.组合总和 Ⅳ
2022-08-01 12:21:00 【小城老街】
1.题目
给你一个由不同整数组成的数组 nums ,和一个目标整数 target 。请你从 nums 中找出并返回总和为 target 的元素组合的个数。
题目数据保证答案符合 32 位整数范围。
示例 1:
输入:nums = [1,2,3], target = 4
输出:7
解释:
所有可能的组合为:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
请注意,顺序不同的序列被视作不同的组合。
示例 2:
输入:nums = [9], target = 3
输出:0
提示:
1 <= nums.length <= 200
1 <= nums[i] <= 1000
nums 中的所有元素互不相同
1 <= target <= 1000
进阶:如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件?
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/combination-sum-iv
2.思路
(1)动态规划
本题可以看作70.爬楼梯这题的升级版:
① 将 target 看作楼梯总台阶数;
② 数组 nums 中的元素是每次上楼梯时可爬的台阶数,例如 nums = [1,2,3] 表示每次上楼梯可以选择爬 1 或 2 或 3 个台阶;
③ 最终到达楼顶所爬的台阶数应该正好等于 target;
④ 现在要计算有多少种不同的方法可以爬到楼顶。
先定义 dp 数组,令 dp[i] 表示爬到第 i 阶楼梯的方法数,其中 1 ≤ i ≤ target。那么按照70.爬楼梯这题中的思路可以推出本题的状态转移方程为
dp[i] = dp[i] + dp[i - num],其中 num 是数组 nums 中所有小于等于 i 的元素。
相似题目:
LeetCode_回溯_中等_39.组合总和
LeetCode_回溯_中等_40.组合总和 II
LeetCode_回溯_中等_216.组合总和 III
3.代码实现(Java)
//思路1————动态规划
class Solution {
public int combinationSum4(int[] nums, int target) {
// dp[i] 表示从 nums 中找出并返回总和为 i 的元素组合的个数
int[] dp = new int[target + 1];
// 初始化
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int num : nums) {
if (num <= i) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
边栏推荐
- 阿里云官方 Redis 开发规范
- CloudCompare&PCL ICP配准(点到面)
- SQL函数 STR
- Detailed explanation of table join
- SCHEMA solves the puzzle
- Do wildcard SSL certificates not support multiple domains?
- [Nodejs] fs module of node
- The use of Ts - Map type
- 蔚来又一新品牌披露:产品价格低于20万
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
猜你喜欢

Audio and Video Technology Development Weekly | 256

How to use DevExpress controls to draw flowcharts?After reading this article, you will understand!

库函数的模拟实现(strlen)(strcpy)(strcat)(strcmp)(strstr)(memcpy)(memmove)(C语言)(VS)

稀疏表示--学习笔记

Find objects with the same property value Cumulative number Summarize

Alibaba Cloud Official Redis Development Specification

Beyond Compare 4 trial period expires

bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)

安全又省钱,“15岁”老小区用上管道燃气

Pytest e-commerce project combat (below)
随机推荐
批量任务导入到数据库中
如何设计一个分布式 ID 发号器?
深入解析volatile关键字
R语言检验时间序列的平稳性:使用tseries包的adf.test函数实现增强的Dickey-Fuller(ADF)检验、检验时序数据是否具有均值回归特性(平稳性)、具有均值回归特性的案例
(ES6以上以及TS) Map对象转数组
阿里云官方 Redis 开发规范
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
CAN通信的数据帧和远程帧
[CLion] CLion always prompts "This file does not belong to any project target xxx" solution
R language ggplot2 visualization: use the ggdensity function of the ggpubr package to visualize density plots, use the stat_central_tendency function to add mean vertical lines to the density and cust
win10系统重装,无法登录进行同步的情况下chrome数据恢复
Feign 从注册到调用原理分析
Data frame and remote frame of CAN communication
软件设计师考点汇总(室内设计师个人总结)
音视频技术开发周刊 | 256
大中型网站列表页翻页过多怎么优化?
SQL函数 STR
Programmer's self-cultivation
R语言拟合ARIMA模型:使用forecast包中的auto.arima函数自动搜索最佳参数组合、模型阶数(p,d,q)、设置seasonal参数指定在模型中是否包含季节信息
Grafana 9.0 released, Prometheus and Loki query builders, new navigation, heatmap panels and more!