dynamic-programming

Dynamic programming - Largest square block

限于喜欢 提交于 2019-11-26 17:04:38
I need to find the largest square of 1's in a giant file full of 1's and 0's. I know i have to use dynamic programming. I am storing it in a 2D array. Any help with the algorithm to find the largest square would be great, thanks! example input: 1 0 1 0 1 0 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 answer: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 My code so far: int Square (Sq[int x][int y]) { if (Sq[x][y]) == 0) { return 0; } else { return 1+MIN( Sq(X-1,Y), Sq(X,Y-1), Sq(X-1,Y-1) ); } } (assuming values already entered into the array) int main() { int Sq[5][6]; //5,6 = bottom right conner int X =

Algorithm to Divide a list of numbers into 2 equal sum lists

雨燕双飞 提交于 2019-11-26 15:26:21
问题 There is a list of numbers. The list is to be divided into 2 equal sized lists, with a minimal difference in sum. The sums have to be printed. #Example: >>>que = [2,3,10,5,8,9,7,3,5,2] >>>make_teams(que) 27 27 Is there an error in the following code algorithm for some case? How do I optimize and/or pythonize this? def make_teams(que): que.sort() if len(que)%2: que.insert(0,0) t1,t2 = [],[] while que: val = (que.pop(), que.pop()) if sum(t1)>sum(t2): t2.append(val[0]) t1.append(val[1]) else: t1

What is the difference between memoization and dynamic programming?

与世无争的帅哥 提交于 2019-11-26 13:57:33
What is the difference between memoization and dynamic programming? I think dynamic programming is a subset of memoization. Is it right? What is difference between memoization and dynamic programming? Memoization is a term describing an optimization technique where you cache previously computed results, and return the cached result when the same computation is needed again. Dynamic programming is a technique for solving problems of recursive nature, iteratively and is applicable when the computations of the subproblems overlap. Dynamic programming is typically implemented using tabulation, but

How to count integers between large A and B with a certain property?

跟風遠走 提交于 2019-11-26 11:45:53
问题 In programming contests, the following pattern occurs in a lot of tasks: Given numbers A and B that are huge (maybe 20 decimal digits or more), determine the number of integers X with A ≤ X ≤ B that have a certain property P SPOJ has lots of tasks like these for practice. Where examples of interesting properties include: \"the digit sum of X is 60\" \"X consists only of the digits 4 and 7\" \"X is palindromic\", which means that the decimal representation of X equals its reverse (for example,

The minimum number of coins the sum of which is S

醉酒当歌 提交于 2019-11-26 11:04:49
问题 Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it\'s not possible to select coins in such a way that they sum up to S. I try to understand dynamic programming, haven\'t figured it out. I don\'t understand the given explanation, so maybe you can throw me a few hints how to program this task? No code, just ideas where I should start. Thanks.

How to split a string into words. Ex: “stringintowords” -> “String Into Words”?

柔情痞子 提交于 2019-11-26 10:24:06
问题 What is the right way to split a string into words ? (string doesn\'t contain any spaces or punctuation marks) For example: \"stringintowords\" -> \"String Into Words\" Could you please advise what algorithm should be used here ? ! Update: For those who think this question is just for curiosity. This algorithm could be used to camеlcase domain names (\"sportandfishing .com\" -> \"SportAndFishing .com\") and this algo is currently used by aboutus dot org to do this conversion dynamically. 回答1:

Subset Sum algorithm

吃可爱长大的小学妹 提交于 2019-11-26 10:21:36
I am working on this problem: The Subset Sum problem takes as input a set X = {x1, x2 ,…, xn} of n integers and another integer K . The problem is to check if there exists a subset X' of X whose elements sum to K and finds the subset if there's any. For example, if X = {5, 3, 11, 8, 2} and K = 16 then the answer is YES since the subset X' = {5, 11} has a sum of 16 . Implement an algorithm for Subset Sum whose run time is at least O(nK) . Notice complexity O(nK) . I think dynamic programming may help. I have found an exponential time algorithm, but it doesn't help. Can someone help me solve

how to find longest palindromic subsequence?

。_饼干妹妹 提交于 2019-11-26 10:17:10
问题 Here is the problem (6.7 ch6 ) from Algorithms book (by Vazirani) that slightly differs from the classical problem that finding longest palindrome. How can I solve this problem ? A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Devise an algorithm that takes a sequence

Box stacking problem

*爱你&永不变心* 提交于 2019-11-26 08:47:46
问题 I found this famous dp problem in many places, but I can not figure out how to solve. You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possible, but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box

What is dynamic programming? [closed]

白昼怎懂夜的黑 提交于 2019-11-26 06:50:19
问题 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 months ago . What is dynamic programming ? How is it different from recursion, memoization, etc? I have read the wikipedia article on it, but I still don\'t really understand it. 回答1: Dynamic programming is when you use past knowledge to make solving a future problem easier. A good example is