dynamic-programming

Integer partitioning in Java

这一生的挚爱 提交于 2021-02-20 04:14:48
问题 I'm trying to implement a program that returns the number of existing partitions of an integer n as part of an assignment. I wrote the code below, but it returns the wrong number (Partitions n returns the result of Partitions n-1). I don't get why this happens. I've tried many things and still don't know how to fix it, can anyone please help me? [edited code out to avoid plagiarism from my colleagues :p] m stands for the biggest number allowed in a partition, so partition(4,4) would be 5 = 4,

Knapsack 0-1 path reconstruction (which items to take) [duplicate]

情到浓时终转凉″ 提交于 2021-02-19 05:37:20
问题 This question already has answers here : How to find which elements are in the bag, using Knapsack Algorithm [and not only the bag's value]? (4 answers) Closed 6 days ago . I know how to solve knapsack 0-1 problem with dynamic programming approach, but I am having troubles figuring out which items to take without compromising the complexity of O(N * C) (N items, C capacity). Any ideas (I would prefer a bottom-up approach)? 回答1: Suppose, right now you're storing results in array bool[] a ,

How do I find largest valid sequence of parentheses and brackets in a string?

笑着哭i 提交于 2021-02-18 12:47:15
问题 So I have a script I need to write and one of the largest problems boils down to finding the largest valid subsequence within a string. So I have something like "()(({}[](][{[()]}]{})))(" as an input and I would need to return "[{[()]}]{}" as an output. I have tried using a stack like structure like you would do if it was just parentheses but haven't been able to figure out something that works. I'd prefer a solution in python but any guidance anyone can offer will help regardless of language

Find the length of the longest valid parenthesis sequence in a string, in O(n) time

不问归期 提交于 2021-02-17 10:30:42
问题 My friend ran into a question in an interview and he was told that there is an O(n) solution. However, neither of us can think it up. Here is the question: There is a string which contains just ( and ) , find the length of the longest valid parentheses substring, which should be well formed. For example ")()())" , the longest valid parentheses is ()() and the length is 4. I figured it out with dynamic programming, but it is not O(n). Any ideas? public int getLongestLen(String s) { if (s ==

Finding the maximum sum that can be formed from a set, by partitioning it into two subset

一世执手 提交于 2021-02-17 02:55:08
问题 Decription Given a set of numbers S. Find maximum sum such that Sum(A 1 ) = Sum(A 2 ) Where, A 1 ⊂S and A 2 ⊂S and A 1 ⋂A 2 =∅ And Sum(X), is the sum of all elements within the set X. Approach Brute Force The easiest approach is: print maximumSum(0,0,0) def maximumSum(index,sum1,sum2): ans=0 if sum1 == sum2: ans=sum1 if index >= len(S): return ans m1=maximumSum(index+1,sum1+S[index],sum2) m2=maximumSum(index+1,sum1,sum2+S[index]) m3=maximumSum(index+1,sum1,sum2) return max(m,m1,m2,m3) Time

Finding the maximum sum that can be formed from a set, by partitioning it into two subset

那年仲夏 提交于 2021-02-17 02:54:07
问题 Decription Given a set of numbers S. Find maximum sum such that Sum(A 1 ) = Sum(A 2 ) Where, A 1 ⊂S and A 2 ⊂S and A 1 ⋂A 2 =∅ And Sum(X), is the sum of all elements within the set X. Approach Brute Force The easiest approach is: print maximumSum(0,0,0) def maximumSum(index,sum1,sum2): ans=0 if sum1 == sum2: ans=sum1 if index >= len(S): return ans m1=maximumSum(index+1,sum1+S[index],sum2) m2=maximumSum(index+1,sum1,sum2+S[index]) m3=maximumSum(index+1,sum1,sum2) return max(m,m1,m2,m3) Time

Generating an optimal binary search tree (Cormen)

我怕爱的太早我们不能终老 提交于 2021-02-08 13:53:15
问题 I'm reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in Python. Here is the example I'm trying to apply the optimal BST to: Let us define e[i,j] as the expected cost of searching an optimal binary search tree containing the keys labeled from i to j . Ultimately, we wish to compute e[1, n] , where n is the number of keys (5 in this example). The

Finding the minimum value

六月ゝ 毕业季﹏ 提交于 2021-02-08 09:09:12
问题 I can't begin to understand how to approach this problem. Can someone help me to just point me in the direction as to how I can approach it? N tasks are given and there are M workers that are available. Each worker can takes different times to complete each task. The time taken by each worker for every task is given. At any time only one task can be worked on by only one worker. But the condition is once a worker has stopped working, he can't work on any task again. I want to find out what is

Minimum coin change problem with limited amount of coins

我的梦境 提交于 2021-02-08 04:46:56
问题 To be specific, the problem is: Given array of denominations coins[] , array of limit for each coins limits[] and number amount , return minimum number of coins needed, to get the amount , or if it's not possible return null. Additionally fill array change with number of each coin used in the solution. This is my solution: public static int? Dynamic(int amount, int[] coins, int[] limits, out int[] change) { int[] minCoins = new int[amount + 1]; int[,] coinsUsedToAmount = new int[coins.Length,

Maximum subarray of size HxW within a 2D matrix

心已入冬 提交于 2021-02-08 03:45:56
问题 Given a 2-dimensional array of positive integers, find the subrectangle of size HxW with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. Input: A 2D array NxN with positive elements The HxW size of the subrectangle Output: The submatrix of HxW size with the largest sum of its elements. I've solved this using a brute-force method, however, I'm now looking for a better solution with better complexity (my brute-force method's complexity is O(n 6 )). 回答1: