dynamic-programming

Dynamic Programming: Tennis Balls, Lockers, and Keys [closed]

北城以北 提交于 2019-12-12 04:05:49
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am having trouble finding the Optimal Dynamic Programming solution to a problem and would greatly appreciate any help. The optimal solution is O(T^2+M); I can only find a solution which is O(NM^2). The problem is: There are N lockers that are labeled 1,2...N. Each locker is

bipartite graph--Sellers and Buyers

♀尐吖头ヾ 提交于 2019-12-12 03:47:19
问题 Given several sellers and buyers, each seller has an amount of products, each buyer wants to buy several products from sellers. Some sellers can not transact with some buyers, if a buyer can not get enough products as he wants, the transaction will not succeed. If we know there is one strategy that can satisfy all buyers, how to find this strategy? I draw a graph to illustrate the problem, this is just an example. The question wants you to provide a transaction strategy for each buyer so that

The last algorithmic dance

做~自己de王妃 提交于 2019-12-12 03:06:26
问题 [ Previous related question: A dance with an aglorithm's complexity ] The story: I'm about to take part in a dance contest with n songs in a specific order. I can't dance to every single song, though, because I need time to prepare before each dance, and time to rest afterward. (Fortunately, the rest time from one dance can overlap with the preparation time for the next.) I know what score I can get for each song I do dance to, and I want to maximize my total score. So, I have three arrays:

How would you create a multidimensional array with n dimensions in Swift?

廉价感情. 提交于 2019-12-12 02:40:21
问题 For instance, asume var hierarchicalFileSystem: [[String]] = [] This allows one to support one layer of folders, but there appears to be no way to create an array in Swift like the one above but with an undefined number of nested String arrays. Am I missing something here? 回答1: An array of arrays (of arrays of arrays...) of strings doesn't really make much sense to represent a file system. What I'd instead recommend is making a class or struct to represent objects in the file system. Perhaps

Longest Common Substring using Recursion and DP

时光毁灭记忆、已成空白 提交于 2019-12-12 02:32:38
问题 I'm trying to find the Longest Common Substring of two strings using Recursion and DP. Please note that I'm not referring to Longest Contiguous subsequence. So, if the two strings were String s1 = "abcdf"; String s2 = "bzcdf" Longest Common Substring == "cdf" (not "bcdf"). Basically they have to be continuous elements I am trying to do this using recursion and backtracking. However, the problem is that if I use a recursion such as below, the +1 are added upfront in a frame, that is higher up

How to find the minimum sum of range of k partitions of an array of size n?

那年仲夏 提交于 2019-12-12 02:19:09
问题 There is an array A of size n. We are given the number of partitions k. How to find the minimum sum of range of all partitions? Range is difference between maximum and mimimum element in that partition. For eg. A={20, 30, 40, 41, 45}, k = 4. The minimum possible sum of range of all partitions is 1. 来源: https://stackoverflow.com/questions/40188492/how-to-find-the-minimum-sum-of-range-of-k-partitions-of-an-array-of-size-n

Minimum number of coins for a given sum and denominations

落爺英雄遲暮 提交于 2019-12-12 01:27:51
问题 Given a set of denominations and the sum required I have to find the minimum number of coins that make that sum and also the number of coins for each denominations Please help!! 回答1: The pseudo-code to determine minimum number of coins needed to make that sum is: Procedure coinChange(coins, total): n := coins.length dp[n][total + 1] for i from 0 to n dp[i][0] := 0 end for for i from 1 to (total + 1) dp[0][i] := i end for for i from 1 to n for j from 1 to (total + 1) if coins[i] > j //if the

how to find the maximum L sum in a matrix?

陌路散爱 提交于 2019-12-12 01:10:03
问题 here is another dynamic programming problem that find the maximum L(chess horse - 4 item) sum in the given matrix (m x n) For example : 1 2 3 4 5 6 7 8 9 L : (1,2,3,6), (1,4,5,6), (1,2,5,8), (4,5,6,9) ... and the biggest sum is sum(L) = sum(7,8,9,6) = 30 what is the O(complexity) of the optimal solution ? it looks like this problem (submatrix with maximum sum) Say all items are positive Both positive and negative Any ideas are welcome! 回答1: If your L is constant size (4 elements, as you say),

Julia pmap speed - parallel processing - dynamic programming

馋奶兔 提交于 2019-12-11 23:20:31
问题 I am trying to speed up filling in a matrix for a dynamic programming problem in Julia (v0.6.0), and I can't seem to get much extra speed from using pmap . This is related to this question I posted almost a year ago: Filling a matrix using parallel processing in Julia. I was able to speed up serial processing with some great help then, and I'm now trying to get extra speed from parallel processing tools in Julia. For the serial processing case, I was using a 3-dimensional matrix (essentially

Find sum of subsequences when passed to function F

血红的双手。 提交于 2019-12-11 22:05:22
问题 Given an array of size N and integer K we need to find the summation of output of all the subsequences of length K when given to function F where Function F is returning sum of the given subsequence * GCD the given subsequence. Example : Let N=2 and K=1 and array has only two elements [1,2] then here answer is 5 as there are only two subsequence : [1],[2]. Output for subsequence [1] from function F is 1*1 = 1 Output for subsequence [2] from function F is 2*2 = 4 So total is 1+4=5. How to find