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>& ...
随机推荐
- JPanel设置图片
package com.gr.db; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class We ...
- zoj3882 博弈
我理解错题目意思,稀里糊涂A了.其实就是先手必胜. #include<stdio.h> int main() { int n; while(scanf("%d",&am ...
- replace和replaceAll的区别
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(Cha ...
- C 文件读写1
打开文件 fopen( ) 函数来创建或者打开文件,这个调用会初始化一个FILE 类型的对象. 原型 FILE *fopen( const char * filename, const char * ...
- DataSet筛选数据然后添加到新的DataSet中引发的一系列血案
直入代码: var ds2 = new DataSet(); ) { ].Select(" usertype <> 'UU'"); ) { DataTable tmp ...
- CMD修复
应该命令的路径被修改了. 试下在cmd下打入 path 命令看看.以下是正确的显示. PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\ ...
- 使用SubLineText3
一 Sublinetext3 1. Sublime Text3是一款跨平台的编辑器, 2. 安装网址: http://www.sublimetext.com/3 二 常用使用方法 1)打开控制台: V ...
- linux建立用户 详细
.你同时属于两个或两个以上的组. 两个条件你至少具备一个,你才能够把文件所属旧组变为新组.使用如下的命令将当前目录下所有html文件所属的组改为httpd: chgrp httpd *.html 和c ...
- JS 下拉菜单
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- JS 内部传参