您的位置:首页 > 其它

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
2
solutions:
[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: