k Sum | & ||

时间:2023-03-09 08:37:36
k Sum | & ||

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[];
helper(A, , k, , target, , total);
return total[];
} 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[]++;
} helper(A, index + , k, count, target, total, kk);
total -= A[index];
count--;
helper(A, index + , k, count, target, total, kk);
}
}

很明显,the preferred approach is DP. 但是如何做呢?我做不出来。 :-( 还是直接copy paste其它牛人的解答吧。

k Sum | & ||
 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 < ) return ;
int len = A.length;
int[][][] D = new int[len + ][k + ][target + ]; for (int i = ; i <= len; i++) {
for (int j = ; j <= k; j++) {
for (int t = ; t <= target; t++) {
if (j == && t == ) {
// select 0 number from i to the target: 0
D[i][j][t] = ;
} else if (!(i == || j == || t == )) {
D[i][j][t] = D[i - ][j][t];
if (t - A[i - ] >= ) {
D[i][j][t] += D[i - ][j - ][t - A[i - ]];
}
}
}
}
}
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]
]
 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 == || k == ) return allList; helper(allList, list, , A, k, , target, );
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 + , A, k, count, target, total);
total -= list.get(list.size() - );
list.remove(list.size() - );
count--;
helper(allList, list, index + , A, k, count, target, total);
}
}

Reference:

http://www.cnblogs.com/yuzhangcmu/p/4279676.html