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>& ...
随机推荐
- Android intent传递list或对象
方法一: 如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 Java代码 intent.putStringArrayListExtra ...
- HDU-1754I Hate It 线段树区间最值
这道题比较基本,就是用线段树维护区间最值,可以算是模板吧-.. I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768 ...
- bzoj1069 SCOI2007 最大土地面积
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2560 Solved: 983 Description ...
- Erlang练习-UDP
贴一下代码,例子是从别人那里直接抄来的: -module(myudp). -export([start/0, client/1]). %% Server start() -> spawn(fun ...
- 将ECSHOP会员注册页面的Email修改成非必填项
将ECSHOP会员注册页面的Email修改成非必填项 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2011-07-29 有人说,在后台的 “会员注册项设置 ”里面 ...
- JDBCTemplate基础学习
JDBCTemplate:spring提供的用于操作数据库的模板,类似DbUtils.使用时必须设置数据源(DataSource):数据源如DBCP.C3P0等 一.JDBCAPI简单使用Demo 1 ...
- sql 语句:给 text 数据类型排序
sql 语句: select * from 表 order by cast(字段 as varchar)
- 安装TFS2008最终版(转载)
一.安装操作系统:windows server 2003 + Sp2具体步骤: 1.安装windows server 2003时选用工作组(默认为workgroup).由于在工作组环境中部署,因此使用 ...
- Visual Studio Online Integrations-Productivity
原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-di ...
- Java-优秀博客推荐
一. TCP/IP Socket 兰亭风雨的专栏: http://blog.csdn.net/ns_code 二. NIO 并发编程网-Java NIO系列教程:http://ifeve.com/ch ...