dynamic-programming

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

可紊 提交于 2019-11-28 20:39:22
问题 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

How can I find the minimum index of the array in this case?

我与影子孤独终老i 提交于 2019-11-28 19:56:52
We are given an array with n values. Example: [1,4,5,6,6] For each index i of the array a ,we construct a new element of array b such that, b[i]= [a[i]/1] + [a[i+1]/2] + [a[i+2]/3] + ⋯ + [a[n]/(n−i+1)] where [.] denotes the greatest integer function. We are given an integer k as well. We have to find the minimum i such that b[i] ≤ k . I know the brute-force O(n^2) algorithm (to create the array - 'b'), can anybody suggest a better time complexity and way solve it? For example, for the input [1,2,3] , k=3 , the output is 1(minimum-index) . Here, a[1]=1; a[2]=2; a[3]=3; Now, b[1] = [a[1]/1] + [a

String Reduction - Programming Contest . Solution needed

*爱你&永不变心* 提交于 2019-11-28 19:54:18
I have a question which asks us to reduce the string as follows. The input is a string having only A , B or C . Output must be length of the reduced string The string can be reduced by the following rules If any 2 different letters are adjacent, these two letters can be replaced by the third letter. Eg ABA -> CA -> B . So final answer is 1 (length of reduced string) Eg ABCCCCCCC This doesn't become CCCCCCCC , as it can be reduced alternatively by ABCCCCCCC -> AACCCCCC -> ABCCCCC -> AACCCC -> ABCCC -> AACC -> ABC -> AA as here length is 2 < (length of CCCCCCCC ) How do you go about this problem

Stuck with an interview Question… Partitioning of an Array

爷,独闯天下 提交于 2019-11-28 17:58:45
I found the following problem on the internet, and would like to know how I would go about solving it: Problem: Integer Partition without Rearrangement Input: An arrangement S of non negative numbers {s 1 , . . . , s n } and an integer k. Output: Partition S into k or fewer ranges, to minimize the maximum of the sums of all k or fewer ranges, without reordering any of the numbers.* Please help, seems like interesting question... I actually spend quite a lot time in it, but failed to see any solution.. Let's try to solve the problem using dynamic programming. Note: If k > n we can use only n

Finding all paths down stairs?

狂风中的少年 提交于 2019-11-28 17:38:18
I was given the following problem in an interview: Given a staircase with N steps, you can go up with 1 or 2 steps each time. Output all possible way you go from bottom to top. For example: N = 3 Output : 1 1 1 1 2 2 1 When interviewing, I just said to use dynamic programming. S(n) = S(n-1) +1 or S(n) = S(n-1) +2 However, during the interview, I didn't write very good code for this. How would you code up a solution to this problem? Thanks indeed! Nikita Rybak You can generalize your recursive function to also take already made moves. void steps(n, alreadyTakenSteps) { if (n == 0) { print

Largest rectangular sub matrix with the same number

有些话、适合烂在心里 提交于 2019-11-28 16:07:34
I am trying to come up with a dynamic programming algorithm that finds the largest sub matrix within a matrix that consists of the same number: example: {5 5 8} {5 5 7} {3 4 1} Answer : 4 elements due to the matrix 5 5 5 5 TMS This is a question I already answered here (and here , modified version). In both cases the algorithm was applied to binary case (zeros and ones), but the modification for arbitrary numbers is quite easy (but sorry, I keep the images for the binary version of the problem ). You can do this very efficiently by two pass linear O(n) time algorithm - n being number of

minimum number of steps to reduce number to 1

强颜欢笑 提交于 2019-11-28 15:52:10
问题 Given any number n, and three operations on n: add 1 subtract 1 divide by 2 if the number is even I want to find the minimum number of the above operations to reduce n to 1. I have tried dynamic progamming approach, also BFS with pruning, but n can be very large (10^300) and I do not know how to make my algorithm faster. The greedy approach (divide by 2 if even and subtract 1 if odd) also does not give the optimal result. Is another solution exists? 回答1: There is a pattern which allows you to

Dynamic programming: Find longest subsequence that is zig zag

和自甴很熟 提交于 2019-11-28 15:49:40
Can anyone please help me understand the core logic behind the solution to a problem mentioned at http://www.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 A zig zag sequence is one that alternately increases and decreases. So, 1 3 2 is zig zag, but 1 2 3 is not. Any sequence of one or two elements is zig zag. We need to find the longest zig zag subsequence in a given sequence. Subsequence means that it is not necessary for elements to be contiguous, like in the longest increasing subsequence problem. So, 1 3 5 4 2 could have 1 5 4 as a zig zag subsequence. We are interested in the

How is dynamic programming different from greedy algorithms?

依然范特西╮ 提交于 2019-11-28 15:48:21
In the book I am using Introduction to the Design & Analysis of Algorithms , dynamic programming is said to focus on the Principle of Optimality , "An optimal solution to any instance of an optimization problem is composed of optimal solutions to its subinstances". Whereas, the greedy technique focuses on expanding partially constructed solutions until you arrive at a solution for a complete problem. It is then said, it must be "the best local choice among all feasible choices available on that step". Since both involve local optimality, isn't one a subset of the other? Dynamic programming is

Building bridges problem - how to apply longest increasing subsequence?

不羁岁月 提交于 2019-11-28 15:32:25
问题 The building bridges problem is stated as follows: There is a river that runs horizontally through an area. There are a set of cities above and below the river. Each city above the river is matched with a city below the river, and you are given this matching as a set of pairs. You are interested in building a set of bridges across the river to connect the largest number of the matching pairs of cities, but you must do so in a way that no two bridges intersect one another. Devise an algorithm