dynamic-programming

Finding 2 equal sum sub-sequences, with maximum sum?

我是研究僧i 提交于 2019-11-30 15:18:12
I have removed all the storylines for this question. Q. You are given N numbers. You have to find 2 equal sum sub-sequences, with maximum sum. You don't necessarily need to use all numbers. Eg 1:- 5 1 2 3 4 1 Sub-sequence 1 : 2 3 // sum = 5 Sub-sequence 2 : 4 1 // sum = 5 Possible Sub-sequences with equal sum are {1,2} {3} // sum = 3 {1,3} {4} // sum = 4 {2,3} {4,1} // sum = 5 Out of which 5 is the maximum sum. Eg 2:- 6 1 2 4 5 9 1 Sub-sequence 1 : 2 4 5 // sum = 11 Sub-sequence 2 : 1 9 1 // sum = 11 The maximum sum you can get is 11 Constraints: 5 <= N <= 50 1<= number <=1000 sum of all

How do I find the closest possible sum of an Array's elements to a particular value?

冷暖自知 提交于 2019-11-30 15:07:38
问题 In Java, how should I find the closest (or equal) possible sum of an Array's elements to a particular value K? For example, for the array {19,23,41,5,40,36} and K=44, the closest possible sum is 23+19=42. I've been struggling on this for hours; I know almost nothing about dynamic programming. By the way, the array contains only positive numbers. 回答1: You would typically use dynamic programming for such a problem. However, that essentially boils down to keeping a set of possible sums and

Sum of products of elements of all subarrays of length k

时光怂恿深爱的人放手 提交于 2019-11-30 14:20:49
An array of length n is given. Find the sum of products of elements of the sub-array. Explanation Array A = [2, 3, 4] of length 3 . Sub-array of length 2 = [2,3], [3,4], [2,4] Product of elements in [2, 3] = 6 Product of elements in [3, 4] = 12 Product of elements in [2, 4] = 8 Sum for subarray of length 2 = 6+12+8 = 26 Similarly, for length 3 , Sum = 24 As, products can be larger for higher lengths of sub-arrays calculate in modulo 1000000007 . What is an efficient way for finding these sums for subarrays of all possible lengths, i.e., 1, 2, 3, ......, n where n is the length of the array. We

How to tell if greedy algorithm suffices for finding minimum coin change?

时间秒杀一切 提交于 2019-11-30 14:14:31
问题 The minimum coin change problem is an NP-complete problem but for certain sets of coins the greedy algorithm (choose largest denominations first) works. Given a set of integers denoting coin-values, what's the fastest algorithm to determine if the greedy algorithm suffices or not? One obvious way is to build up your dynamic programming solution till the largest denomination and see for each if it yields a better solution than the greedy way. But is there a faster "math-way" of detecting it?

How do I find the closest possible sum of an Array's elements to a particular value?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 13:22:13
In Java, how should I find the closest (or equal) possible sum of an Array's elements to a particular value K? For example, for the array {19,23,41,5,40,36} and K=44, the closest possible sum is 23+19=42. I've been struggling on this for hours; I know almost nothing about dynamic programming. By the way, the array contains only positive numbers. You would typically use dynamic programming for such a problem. However, that essentially boils down to keeping a set of possible sums and adding the input values one by one, as in the following code, and has the same asymptotic running time: O(n K) ,

Longest Common Subsequence

Deadly 提交于 2019-11-30 13:18:22
Consider 2 sequences X[1..m] and Y[1..n]. The memoization algorithm would compute the LCS in time O(m*n). Is there any better algorithm to find out LCS wrt time? I guess memoization done diagonally can give us O(min(m,n)) time complexity. Gene Myers in 1986 came up with a very nice algorithm for this, described here: An O(ND) Difference Algorithm and Its Variations . This algorithm takes time proportional to the edit distance between sequences, so it is much faster when the difference is small. It works by looping over all possible edit distances, starting from 0, until it finds a distance for

Memoization Handler

白昼怎懂夜的黑 提交于 2019-11-30 12:52:26
Is it "good practice" to create a class like the one below that can handle the memoization process for you? The benefits of memoization are so great (in some cases, like this one, where it drops from 501003 to 1507 function calls and from 1.409 to 0.006 seconds of CPU time on my computer) that it seems a class like this would be useful. However, I've read only negative comments on the usage of eval() . Is this usage of it excusable, given the flexibility this approach offers? This can save any returned value automatically at the cost of losing side effects. Thanks. import cProfile class

Minimum Edit Distance Reconstruction

≯℡__Kan透↙ 提交于 2019-11-30 12:16:13
问题 I know there are similar answer to this on stack, as well as online, but I feel I'm missing something. Given the code below, we need to reconstruct the sequence of events that led to the resulting minimum edit distance. For the code below, we need to write a function that outputs: Equal, L, L Delete, E Equal, A, A Substitute, D, S Insert, T EDIT: CODE IS UPDATED WITH MY (PARTIALLY CORRECT) SOLUTION Here is the code, with my partial solution. It works for example I was given ("lead" -> "last")

Number of n-element permutations with exactly k inversions

浪子不回头ぞ 提交于 2019-11-30 12:03:04
问题 I am trying to efficiently solve SPOJ Problem 64: Permutations. Let A = [a1,a2,...,an] be a permutation of integers 1,2,...,n. A pair of indices (i,j), 1<=i<=j<=n, is an inversion of the permutation A if ai>aj. We are given integers n>0 and k>=0. What is the number of n-element permutations containing exactly k inversions? For instance, the number of 4-element permutations with exactly 1 inversion equals 3. To make the given example easier to see, here are the three 4-element permutations

Implementing Text Justification with Dynamic Programming

社会主义新天地 提交于 2019-11-30 11:57:03
问题 I'm trying to understand the concept of Dynamic Programming, via the course on MIT OCW here. The explanation on OCW video is great and all, but I feel like I don't really understand it until I implemented the explanation into code. While implementiing, I refer to some notes from the lecture note here, particularly page 3 of the note. The problem is, I have no idea how to translate some of the mathematical notation to code. Here's some part of the solution I've implemented (and think it's