k Sum | & ||
2016-07-05 00:55
274 查看
k Sum
Given n distinct positive integers, integer k (k <= n) and a number target.Find k numbers where sum is target. Calculate how many solutions there are?
Example
Given
[1,2,3,4], k =
2, target =
5.
There are
2solutions:
[1,4]and
[2,3].
Return
2.
分析:
第一种方法用递归,但是超时了。
public class Solution { public int kSum(int A[], int k, int target) { int[] total = new int[1]; helper(A, 0, k, 0, target, 0, total); return total[0]; } public void helper(int[] A, int index, int k, int count, int target, int total, int[] kk) { if (count > k || index >= A.length || total > target) return; total += A[index]; count++; if (count == k && total == target) { kk[0]++; } helper(A, index + 1, k, count, target, total, kk); total -= A[index]; count--; helper(A, index + 1, k, count, target, total, kk); } }
很明显,the preferred approach is DP. 但是如何做呢?我做不出来。 :-( 还是直接copy paste其它牛人的解答吧。
F[0][0][0]表示在一个空集中找出0个数,target为0,则有1个解,就是什么也不挑嘛! 其实应该这样写,也就是说,找0个数,目标为0,则一定是有1个解:
if (j == 0 && t == 0) {
// select 0 number from i to the target: 0
D[i][j][t] = 1;
}
1. 状态表达式:
D[i][j][t] = D[i - 1][j][t];
if (t - A[i - 1] >= 0) {
D[i][j][t] += D[i - 1][j - 1][t - A[i - 1]];
}
意思就是:
(1)我们可以把当前A[i - 1]这个值包括进来,所以需要加上D[i - 1][j - 1][t - A[i - 1]](前提是t - A[i - 1]要大于0)
(2)我们可以不选择A[i - 1]这个值,这种情况就是D[i - 1][j][t],也就是说直接在前i-1个值里选择一些值加到target.
public class Solution { public int kSum(int A[], int k, int target) { if (target < 0) return 0; int len = A.length; int[][][] D = new int[len + 1][k + 1][target + 1]; for (int i = 0; i <= len; i++) { for (int j = 0; j <= k; j++) { for (int t = 0; t <= target; t++) { if (j == 0 && t == 0) { // select 0 number from i to the target: 0 D[i][j][t] = 1; } else if (!(i == 0 || j == 0 || t == 0)) { D[i][j][t] = D[i - 1][j][t]; if (t - A[i - 1] >= 0) { D[i][j][t] += D[i - 1][j - 1][t - A[i - 1]]; } } } } } return D[len][k][target]; } }
k Sum II
Given n unique integers, number k (1<=k<=n) and target.Find all possible k integers where their sum is target.
Have you met this question in a real interview?
Yes
Example
Given
[1,2,3,4], k =
2, target =
5. Return:
[ [1,4], [2,3] ]
[/code]
public class Solution { public ArrayList<ArrayList<Integer>> kSumII(int[] A, int k, int target) { ArrayList<ArrayList<Integer>> allList = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list = new ArrayList<Integer>(); if (A == null || A.length == 0 || k == 0) return allList; helper(allList, list, 0, A, k, 0, target, 0); return allList; } public void helper(ArrayList<ArrayList<Integer>> allList, ArrayList<Integer> list, int index, int[] A, int k, int count, int target, int total) { if (count > k || index >= A.length || total > target) return; list.add(A[index]); total += A[index]; count++; if (count == k && total == target) { allList.add(new ArrayList<Integer>(list)); } helper(allList, list, index + 1, A, k, count, target, total); total -= list.get(list.size() - 1); list.remove(list.size() - 1); count--; helper(allList, list, index + 1, A, k, count, target, total); } }
Reference:
http://www.cnblogs.com/yuzhangcmu/p/4279676.html
相关文章推荐
- 我的手机管家(7)骚扰拦截 适配器
- Android SDK Manager 中如果没有相应的镜像ARM XX Image
- 桥接模式 详解
- 滴水算法小结
- 树莓派IoT 学习1 学习计划及搭建samba环境
- 树形背包总结
- 32位plsql连接64位oracle
- iOS-NSURLSession类及代理使用详解
- Eclipse Java注释模板设置详解/版权声明及编码风格的注释
- Colorful Image Colorization 的环境配置
- python os.path模块
- logback自定义格式转换器
- Android Studio官方文档之工程项目概述篇
- laravel 控制器里 redirect url地址传两个参数的方法
- Python安装Mysql驱动出错解决(最新出炉)
- Python中PyQuery库的使用总结
- Android SDK无法更新解决方式(非常有效)
- 【leetcode】26. Remove Duplicates from Sorted Array
- 【leetcode】25. Reverse Nodes in k-Group
- 申请成功