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?
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其它牛人的解答吧。
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.
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
k Sum | & ||的更多相关文章
- summary of k Sum problem and solutions in leetcode
I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- K Sum(2 Sum,3 Sum,4 Sum,3-Sum Closest)
算是经典算法问题了.这里主要针对只存在一个解或者只需要求一个解的情况描述一下解题思路.若需要找到所有可能解,方法需要略作调整.如有问题,欢迎指正. 2 sum: 如果已排序,可直接用夹逼法,即两指针从 ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)
目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...
- 南京网络赛 E K Sum
K Sum 终于过了这玩意啊啊啊==== 莫比乌斯反演,杜教筛,各种分块,积性函数怎么线性递推还很迷==,得继续研究研究 #include<bits/stdc++.h> using nam ...
- 2019南京网络赛E:K Sum
Description: 定义函数 \[ f _n (k) = \sum _{l _1 = 1} ^n \sum _{l _2 = 1} ^n \cdots \sum _{l _k = 1} ^n \ ...
- Leetcode - K Sum
List<List<Integer>> kSum_Trim(int[] a, int target, int k) { List<List<Integer>& ...
随机推荐
- tomcat虚拟路径的几种配置方法
一般我们都是直接引用webapps下面的web项目,如果我们要部署一个在其它地方的WEB项目,这就要在TOMCAT中设置虚拟路径了,Tomcat的加载web顺序是先加载 $Tomcat_home$\c ...
- formData_html5_map标签
1 : //更省事 var files = fileInput.files; var formData = new FormData(); //将所有文件插入formData formData .ap ...
- baidu时光轴_使用window.scroll实现的
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...
- 【bzoj1857】 Scoi2010—传送带
http://www.lydsy.com/JudgeOnline/problem.php?id=1857 (题目链接) 题意 给出两条线段AB和CD,在AB上的速度为P,在CD上的速度为Q,在AB,C ...
- 黑客帝国风格必备插件ProPowerTools
ProPowerTools 详细说明点这里
- Linux LSM(Linux Security Modules) Hook Technology
目录 . 引言 . Linux Security Module Framework Introduction . LSM Sourcecode Analysis . LSMs Hook Engine: ...
- groovy-集合
Lists 你能使用下面的方法创建一个lists,注意[]是一个空list. 1 def list = [5, 6, 7, 8] 2 assert list.get(2) == 7 3 assert ...
- C++处理一个动态规划的问题
嗯哼,别人问的问题,看的我也头晕,百度了一下动态规划,看了看才想起来该怎么做,今天写了写代码,实现了~ 要求是递归,动态规划,想了想这种方法也是最简单的~ 所谓动态规划:把多阶段过程转化为一系列单阶段 ...
- APNs详细使用步骤
1. 什么是推送通知 消息通知分本地通知和远程推送通知,是没有运行在前台的应用程序可以让它们的用户获得相关消息通知的方式.消息通知可能是一条消息,即将发生的日历事件,或远程服务器的新数据.当被操作系统 ...
- 锋利的jQuery-2--判断jQuery获取到的对象是否存在$().length
1.使用js获取不存在的对象: document.getElementById("tt").style.color = "red"; 如果网页中不存在id = ...