dynamic-programming

Algorithm to 'count the number of ways a floor of size 5*N can be filled with tiles of sizes 1*5 and 2*5'

白昼怎懂夜的黑 提交于 2019-12-13 07:55:19
问题 Here is the part of the question with copied for ref: *You are given a floor of size 5xN. You have tiles of 2 different sizes: 1x5 and 2x5. Of course, you can rotate the tiles to get 2 more tile sizes: 5x1 and 5x2. You have to do the flooring using these tiles in the following way: Floor space should be completely covered by tiles. You cannot break tiles, ie, you have to use a tile entirely or not at all. Any tile should not extend beyond the floor space. Tiles should be placed parallel to

Updating maximum sum subinteral in an array in sublinear time when an adjacent transposition is applied

白昼怎懂夜的黑 提交于 2019-12-13 04:02:28
问题 I asked this question for general transpositions and it seemed too hard, I only got one answer which didn't seem to give a guaranteed asymptotic speed-up. So suppose we apply a sequence of adjacent transpositions to a numeric array (an adjacent transposition swaps two adjacent numbers) and we want to maintain the solution of the maximum sum subinterval after each adjacent transposition. We could repeat Kadane's linear time solution from scratch on the entire array after every adjacent

Example of non-Euclidean tabulation in Dynamic Programming?

回眸只為那壹抹淺笑 提交于 2019-12-13 03:53:54
问题 In the accepted answer to this SO question regarding Dynamic Programming, the authors mentioned that: Tabulation - You can also think of dynamic programming as a "table-filling" algorithm (though usually multidimensional, this 'table' may have non-Euclidean geometry in very rare cases). What are some examples of this 'non-Euclidean' geometry tabulation the authors referred to? 来源: https://stackoverflow.com/questions/47058809/example-of-non-euclidean-tabulation-in-dynamic-programming

DP Algo - Making an array equal by adding to all but one

自古美人都是妖i 提交于 2019-12-13 03:46:47
问题 Was doing a HackerRank problem: https://www.hackerrank.com/challenges/equal/problem Where the problem is essentially given an array, find the minimum number of additions it takes to make the array equal by adding to all but one element of the array. Possible values to add are 1, 2, 5. So if we have an array of [2,2,3,7] , we can do: Choose i = 3 as the excluded element and add all other by 5: [7,7,8,7] Choose i = 2 as the excluded and add by 1: [8,8,8,8] This gives minimum of 2 additions.

Dynamic Programming - Number of combinations to reach a given target

对着背影说爱祢 提交于 2019-12-13 03:31:35
问题 This is a basic dynamic programming problem - Number of score combinations. I am aware of the bottom up approach for this problem which works well. However, I am unable to work towards finding a top-down solution approach for the problem. Caching the recursive part gives us more than necessary combinations(where ordering/sequence of scores is also a factor, so, to avoid it we need to provide a constraint to make the sequence increase monotonically. Here is recursive approach for the same.

construct large square using smaller squares

无人久伴 提交于 2019-12-13 01:17:23
问题 I have a large square of edge N . I want to calculate the number of small squares required to construct this large square using smaller squares with edge values from 1 to N-1 . I have unlimited number of such small squares.The only restriction is that I have to use the minimum number of smaller squares.For example,if N=3, I can construct this square using 5 squares of size 1 and 1 square of size 2 . How can I solve this problem for any given value of N ? 回答1: For even N , you can use 4

Knapsack - save time and memory

老子叫甜甜 提交于 2019-12-12 23:05:30
问题 According to Wikipedia and other sources I had went through, you need matrix m[n][W] ; n - number of items and W - total capacity of knapsack. This matrix get really big, sometimes too big to handle it in C program. I know that dynamic programming is based on saving time for memory but still, is there any solution where can you save time and memory? Pseudo-code for Knapsack problem: // Input: // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) //

Longest Common Subsequence among 3 Strings

南笙酒味 提交于 2019-12-12 20:43:11
问题 I've implemented the dynamic programming solution to find the longest common subsequence among 2 strings. There is apparently a way to generalize this algorithm to find the LCS among 3 strings, but in my research I have not found any information on how to go about this. Any help would be appreciated. 回答1: To find the Longest Common Subsequence (LCS) of 2 strings A and B, you can traverse a 2-dimensional array diagonally like shown in the Link you posted. Every element in the array corresponds

maximize profit through trading

谁说我不能喝 提交于 2019-12-12 19:36:50
问题 I have been stuck on this question for a while trying to figure out the recurrence relationship for the following problem. Problem description: Suppose in a market the following trade options are possible: 1 metal to 2 wood 1 wood to 0.2 glass 1 glass to 1.5 metal 1 wood to 0.4 fire 1 fire to 3 metal Determine if it's possible to make profit on a certain item simply by trading. For example in the described scenario above, we can make a profit on metal by doing the following: 1 metal -> 2 wood

tower of boxes (stacking cubes)

て烟熏妆下的殇ゞ 提交于 2019-12-12 19:08:16
问题 i got this task last week but can't find a good algorithm to solve the problem. So here is the description: You can build a stable tower with building cubes by not putting bigger cubes to smaller ones and if you don't put harder cubes into lighter ones. Make a programme which gives you the highest possible tower with n number of cubes. Input: In the first row of in.txt there is the number of cubes n (1 =< n =<1000). the following n line consisting two positive integer, a cube's sidelength and