dynamic-programming

Subset sum recursively in Python

这一生的挚爱 提交于 2020-01-30 05:11:09
问题 I will be happy to get some help. I have the following problem: I'm given a list of numbers seq and a target number and I need to write 2 things: A recursive solution that returns True if there is a sum of a subsequence that equals the target number and False otherwise. example: subset_sum([-1,1,5,4],0) # True subset_sum([-1,1,5,4],-3) # False Secondly, I need to write a solution using what I wrote in the previous solution but now with memoization that uses a dictionary in which the keys are

Find all the occurences of a string as a subsequence in a given string

徘徊边缘 提交于 2020-01-25 10:13:35
问题 Given strings A and B, find all the occurrences of B in A as a subsequence. Example: A = "mañana de la mañana" B = "mañana" Answer: 0 -> mañana de la mañana 1 -> mañan a de l a mañana 2 -> mañ ana de la mañ ana ... Based on an algorithm I found here which counts the number of occurrences there are 16 of those. I need an algorithm that finds all such subsequences and reports their indices. 回答1: The basic recursion be like /** * @param {[type]} iA current letter index in A * @param {[type]} iB

How to get the list of selected items in 0-1 knapsack?

十年热恋 提交于 2020-01-24 19:55:46
问题 i have a code of the naive solution of the Knapsack problem, i want to get the list of index of selected items, currently it is returning the total sum of values of the selected items. Any help will be appreciated. JAVA CODE: /* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ /* A Naive recursive implementation of 0-1 Knapsack problem */ class Knapsack { // A

Finding number of subsets of an array that add up to a multiple of a specific number

那年仲夏 提交于 2020-01-24 13:59:05
问题 I have an array A of length N of negative as well as positive integers. I need to count the number of subsets in this array which add up to a multiple of a number M (or 0 (mod M)) For example: Let A = {1,2,8,4,5}, M = 9, Then, there are 4 such subsets: {}: Empty set, corresponding to the multiple 0, {1,8}: corresponding to the multiple 9, {4,5}: corresponding to the multiple 9 {1,8,4,5}: corresponding to the multiple 18. I thought of generating all possible multiples and then applying dynamic

memoization for recursive Longest Increasing subsequence

偶尔善良 提交于 2020-01-24 12:10:07
问题 I came up with simple following recursive solution for Longest increasing sub-sequence. But, Can you help to include memoization into this recursive solution. public int findLIS(int a[], int maxSoFar, int item, int count) { if(item == a.length) { return count; } int length1 = findLIS(a,maxSoFar, item+1, count); int length2 = 0; if(a[item] > maxSoFar) { length2 = findLIS(a, a[item], item+1, count + 1); } return Math.max(length1, length2); } PS: This not a homework question, it is more of my

Distribute numbers to two “containers” and minimize their difference of sum

馋奶兔 提交于 2020-01-23 22:25:14
问题 Suppose there are n numbers let says we have the following 4 numbers 15,20,10,25 There are two container A and B and my job is to distribute numbers to them so that the sum of the number in each container have the least difference. In the above example, A should have 15+20 and B should have 10+ 25. So difference = 0. I think of a method. It seems to work but I don't know why. Sort the number list in descending order first. In each round, take the maximum number out and put to the container

How to find the smallest number with just 0 and 7 which is divided by a given number? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-01-23 02:42:11
问题 This question already has answers here : How to find the smallest number with just 0 and 1 which is divided by a given number? (10 answers) Closed 4 years ago . This is one of algorithmic problem I encountered in one of interview. Unable to figure out how to solve it in most efficient way. 回答1: Here is my suggested code. It finds the smallest number with 0 and 7 (except the number 0) within the long range. In this case, I'm looking for the result for 11. public class Class007 { static long

Find the number of unordered pair in an array

佐手、 提交于 2020-01-21 04:38:05
问题 I ran into an interesting algorithm problem: Given an array of integer, find the number of un-ordered pairs in that array, say given {1, 3, 2}, the answer is 1 because {3, 2} is un-ordered, and for array {3, 2, 1}, the answer is 3 because {3, 2}, {3, 1}, {2, 1}. Obviously, this can be solved by brute force with O(n^2) running time, or permute all possible pairs then eliminate those invalid pairs. My question is does any body have any better solution and how would you do it because it seems

Is this variant of the subset sum problem easier to solve?

落爺英雄遲暮 提交于 2020-01-19 07:39:11
问题 I have a problem related to the subset sum problem and am wondering if the differences make it easier, i.e. solvable in a reasonable amount of time. Given a value V, a set size L, and a sequence of numbers [1,N] S, how many size L subsets of S sum to less than V? This is different than the subset sum problem in three ways: I care how many subsets are less than a given value, not how many are equal . The subset sizes are fixed. I care how many sets sum to less than V, not just whether any

Is this variant of the subset sum problem easier to solve?

江枫思渺然 提交于 2020-01-19 07:38:13
问题 I have a problem related to the subset sum problem and am wondering if the differences make it easier, i.e. solvable in a reasonable amount of time. Given a value V, a set size L, and a sequence of numbers [1,N] S, how many size L subsets of S sum to less than V? This is different than the subset sum problem in three ways: I care how many subsets are less than a given value, not how many are equal . The subset sizes are fixed. I care how many sets sum to less than V, not just whether any