dynamic-programming

Dynamic programming - Largest square block

人走茶凉 提交于 2019-11-26 05:59:32
问题 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) ); } }

Find common substring between two strings

与世无争的帅哥 提交于 2019-11-26 03:26:32
问题 I\'d like to compare 2 strings and keep the matched, splitting off where the comparison fails. So if I have 2 strings - string1 = apples string2 = appleses answer = apples Another example, as the string could have more than one word. string1 = apple pie available string2 = apple pies answer = apple pie I\'m sure there is a simple Python way of doing this but I can\'t work it out, any help and explanation appreciated. 回答1: Its called Longest Common Substring problem. Here I present a simple,

What is the difference between memoization and dynamic programming?

微笑、不失礼 提交于 2019-11-26 02:59:47
问题 What is the difference between memoization and dynamic programming? I think dynamic programming is a subset of memoization. Is it right? 回答1: 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

What is the difference between bottom-up and top-down?

送分小仙女□ 提交于 2019-11-26 02:15:40
问题 The bottom-up approach (to dynamic programming) consists in first looking at the \"smaller\" subproblems, and then solve the larger subproblems using the solution to the smaller problems. The top-down consists in solving the problem in a \"natural manner\" and check if you have calculated the solution to the subproblem before. I\'m a little confused. What is the difference between these two? 回答1: rev4: A very eloquent comment by user Sammaron has noted that, perhaps, this answer previously

Subset Sum algorithm

时光怂恿深爱的人放手 提交于 2019-11-26 01:58:14
问题 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

How to determine the longest increasing subsequence using dynamic programming?

落爺英雄遲暮 提交于 2019-11-26 00:13:31
问题 I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming. 回答1: OK, I will describe first the simplest solution which is O(N^2), where N is the size of the collection. There also exists a O(N log N) solution, which I will describe also. Look here for it at the section Efficient algorithms. I will assume the indices of the array are from 0 to N - 1. So let's define DP[i] to be the length of the LIS (Longest increasing subsequence) which