dynamic-programming

0/1 Knapsack Dynamic Programming Optimazion, from 2D matrix to 1D matrix

若如初见. 提交于 2019-11-30 05:27:44
I need some clarification from wikipedia: Knapsack , on the part This solution will therefore run in O(nW) time and O(nW) space. Additionally, if we use only a 1-dimensional array m[W] to store the current optimal values and pass over this array i+1 times, rewriting from m[W] to m[1] every time, we get the same result for only O(W) space. I am having trouble understanding how to turn a 2D matrix into a 1D matrix to save space. In addition, to what does rewriting from m[W] to m[1] every time mean (or how does it work). Please provide some example. Say if I have the set {V,W} --> {(5,4),(6,5),(3

Understanding change-making algorithm

一笑奈何 提交于 2019-11-30 04:52:12
I was looking for a good solution to the Change-making problem and I found this code(Python): target = 200 coins = [1,2,5,10,20,50,100,200] ways = [1]+[0]*target for coin in coins: for i in range(coin,target+1): ways[i]+=ways[i-coin] print(ways[target]) I have no problems understanding what the code literally does,but I can't understand WHY it works. Anyone can help? To get all possible sets that elements are equal to 'a' or 'b' or 'c' (our coins) that sum up to 'X' you can: Take all such sets that sum up to X-a and add an extra 'a' to each one. Take all such sets that sum up to X-b and add an

Finding an optimal solution that minimizes a constraint?

亡梦爱人 提交于 2019-11-30 04:13:51
问题 Let us call this problem the Slinger-Bird problem (actually the Slinger is analogous to a server and the bird to a request but I was having a nervous breakdown thinking about it so I changed them hoping to get a different perspective!). There are S stone throwers (slingers) and B birds. The slingers are not within the range of each other. Slinging once can kill all birds within the sight of a slinger and will consume one shot and one time unit I am trying to figure out the optimal solution

How to compute optimal paths for traveling salesman bitonic tour?

孤街浪徒 提交于 2019-11-30 03:59:28
UPDATED After more reading, the solution can be given with the following recurrence relation: (a) When i = 1 and j = 2, l(i; j) = dist(pi; pj ) (b) When i < j - 1; l(i; j) = l(i; j - 1) + dist(pj-1; pj) (c) When i = j - 1 and j > 2, min 1<=k<i (l(k; i) + dist(pk; pj )) This is now starting to make sense, except for part C. How would I go about determining the minimum value k? I suppose it means you can iterate through all possible k values and just store the minimum result of ( l(k,i) + dist(pk,pj)? Yes, definitely a problem I was studying at school. We are studying bitonic tours for the

Generalised Two-Egg Puzzle

老子叫甜甜 提交于 2019-11-30 03:55:55
Here is the Problem Description : Suppose that we wish to know which stories in a N-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: An egg that survives a fall can be used again. A broken egg must be discarded. The effect of a fall is the same for all eggs. If an egg breaks when dropped, then it would break if dropped from a higher window. If an egg survives a fall then it would survive a shorter fall. It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the Nth-floor windows do not cause

Algorithm to bracket an expression in order to maximize its value

不羁岁月 提交于 2019-11-30 03:21:31
问题 I found this while looking up problems on dynamic programming. You are given an unparanthesized expression of the form V0 O0 V1 O1 .... Vn-1 We have to put brackets at places which maximizes the value of the entire expression. V's are the operands and O's are the operators. In the first version of problem operators can be * and + and operands are positive. Second version of problem is completely general. For the first version i came up with DP solution . Solution - A[] = operands array B[] -

Implementing Text Justification with Dynamic Programming

假装没事ソ 提交于 2019-11-30 02:20:19
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 implemented right): import math paragraph = "Some long lorem ipsum text." words = paragraph.split(" ") #

Square Subsequence

て烟熏妆下的殇ゞ 提交于 2019-11-30 02:05:07
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.The subsequence need not be unique. eg string 'aaa' will have 3 square subsequences Observation 1: The length of a square string is always even. Observation 2: Every square subsequence of length 2n

Number of n-element permutations with exactly k inversions

别来无恙 提交于 2019-11-30 01:57:49
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 with exactly 1 inversion: (1, 2, 4, 3) (1, 3, 2, 4) (2, 1, 3, 4) In the first permutation, 4 > 3 and the

Largest submatrix with equal no of 1's and 0's

左心房为你撑大大i 提交于 2019-11-29 23:23:09
Given a matrix of size mxn containing 0's and 1's only. I need to find the largest sub-matrix which has equal number of 1's and 0's in it. Brute force approach would be O(m^2*n^2) Can we do any better than this? I tried applying dynamic programming, but couldn't find any optimal substructure to it. I believe a similar one-dimensional version of this problem was discussed here: Space-efficient algorithm for finding the largest balanced subarray? which has an O(n) solution using some extra space. This algorithm assumes that we search a sub-matrix with contiguous rows and columns and with the