组合和---动态规划法解决问题

该博客介绍了一种使用动态规划解决寻找特定目标数字的候选数组合问题的方法。通过升序排列候选数并遍历所有可能的目标值,博主展示了如何避免重复组合的出现。在示例中,给出了具体算法实现及输出,帮助读者理解动态规划的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给定一组候选数(C)(无重复)和目标数(T),找到候选数与T相加的C中的所有独特组合。  的相同重复数目可以选自c无限次数。

注意:

  • 所有数字(包括目标)将为正整数。
  • 解决方案集不能包含重复的组合。

例如,给定候选集[2, 3, 6, 7]和目标7
A解集是:

[ 
  [7],
  [2,2,3] 
]
动态规划法保存已解决的子问题的答案,而在需要时再找出已求得的答案,这样就可以避免大量的重复计算,节省时间适合于用动态规划法求解的问题,经分解后得到的子问题往往不是互相独立的(即下一个子阶段的求解是建立在上一个子阶段的解的基础上,进行进一步的求解。

算法设计如下:含答案分析

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * 
 * 动态规划法
 *
 */
public class Solution {


public static List<List<Integer>> combinationSum(int[] candidates, int target) {


Arrays.sort(candidates); // 升序排列
List<List<List<Integer>>> dp = new ArrayList<>();
for (int i = 1; i <= target; i++) { // run through all targets from 1 to target
List<List<Integer>> newList = new ArrayList<>(); // combs for curr i
// run through all candidates <= i
for (int j = 0; j < candidates.length && candidates[j] <= i; j++) {
// special case when curr target is equal to curr candidate
if (i == candidates[j])
newList.add(Arrays.asList(candidates[j]));
// if current candidate is less than the target use prev results
else
for (List<Integer> l : dp.get(i - candidates[j] - 1)) {
if (candidates[j] <= l.get(0)) {
System.out.println(candidates[j]+" "+l.get(0));
List<Integer> cl = new ArrayList<>();
cl.add(candidates[j]);
cl.addAll(l);
System.out.println(cl);
newList.add(cl);
}
}
}
System.out.println(newList);
dp.add(newList);
}
return dp.get(target - 1);
}


public static void main(String[] args) {




int [] nums={2, 3, 6, 7};
int target=7;
System.out.println(combinationSum(nums,target));
}


}

结果输出:

[]
[[2]]
[[3]]
2 2
[2, 2]
[[2, 2]]
2 3
[2, 3]
[[2, 3]]
2 2
[2, 2, 2]
3 3
[3, 3]
[[2, 2, 2], [3, 3], [6]]
2 2
[2, 2, 3]
[[2, 2, 3], [7]]
[[2, 2, 3], [7]]

读者可以依据题目输出进行分析本题中动态规划算法的使用方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值