LintCode "k Sum" !!

Great great DP learning experience:
http://www.cnblogs.com/yuzhangcmu/p/4279676.html

Remember 2 steps of DP: a) setup state transfer equation; b) setup selection strategy.

a) States

From problem statement, we find 3 variables: array size, k and target. So it is 3D DP:

dp[i][j][t]: in previous i elements, we pick j of them, to reach value t - number of results

b) Selection Strategy

Usually for 1D array, selection strategy is very simple: with A[i] - pick it or not. Combined with step a, dp[i][j][t] is result of the 2 choices - pick or not:

dp[i][j][t] = dp[i-1][j-1][t-A[i]](pick it) + dp[i-1][j][t](no pick)

Optimization: dimension i can be eliminated.

Details: To start: all dp[*][0][0] = 1

时间: 2024-10-06 07:50:32

LintCode "k Sum" !!的相关文章

lintcode: k Sum 解题报告

k SumShow Result My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted 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? Ex

Lintcode: k Sum II

Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II 1 public class Solution { 2 /** 3

k Sum | &amp; ||

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

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please not

Leetcode - K Sum

List<List<Integer>> kSum_Trim(int[] a, int target, int k) { List<List<Integer>> result = new ArrayList<>(); if (a == null || a.length < k || k < 2) return result; Arrays.sort(a); kSum_Trim(a, target, k, 0, result, new A

[LintCode] Submatrix Sum 子矩阵之和

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Have you met this question in a real interview? Yes Example Given matrix [ [1 ,5 ,7], [3 ,7 ,-8],

Lintcode: Interval Sum II

Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start, end), return the sum from index start to index end in the given array. For modify(index, value), modify the number in t

Lintcode: Interval Sum

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result l

k Sum II

Given n unique integers, number k Example Given [1,2,3,4], k = 2, target = 5. Return: [ [1,4], [2,3] ] public class Solution { /** * @param A: an integer array. * @param k: a positive integer (k <= length(A)) * @param target: a integer * @return a li