dynamic-programming

Maximum possible number of rectangles that can be crossed with a single straight line

守給你的承諾、 提交于 2019-11-29 22:56:07
I found this challenge problem which states the following : Suppose that there are n rectangles on the XY plane. Write a program to calculate the maximum possible number of rectangles that can be crossed with a single straight line drawn on this plane. I have been brainstorming for quite a time but couldn't find any solution. Maybe at some stage, we use dynamic programming steps but couldn't figure out how to start. Here is a sketch of an O(n^2 log n) solution. First, the preliminaries shared with other answers. When we have a line passing through some rectangles, we can translate it to any of

Finding 2 equal sum sub-sequences, with maximum sum?

三世轮回 提交于 2019-11-29 22:00:49
问题 I have removed all the storylines for this question. Q. You are given N numbers. You have to find 2 equal sum sub-sequences, with maximum sum. You don't necessarily need to use all numbers. Eg 1:- 5 1 2 3 4 1 Sub-sequence 1 : 2 3 // sum = 5 Sub-sequence 2 : 4 1 // sum = 5 Possible Sub-sequences with equal sum are {1,2} {3} // sum = 3 {1,3} {4} // sum = 4 {2,3} {4,1} // sum = 5 Out of which 5 is the maximum sum. Eg 2:- 6 1 2 4 5 9 1 Sub-sequence 1 : 2 4 5 // sum = 11 Sub-sequence 2 : 1 9 1 //

Dynamic programming in the functional paradigm

假如想象 提交于 2019-11-29 20:12:19
I'm looking at Problem thirty one on Project Euler, which asks, how many different ways are there of making £2 using any number of coins of 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). There are recursive solutions, such as this one in Scala (credit to Pavel Fatin) def f(ms: List[Int], n: Int): Int = ms match { case h :: t => if (h > n) 0 else if (n == h) 1 else f(ms, n - h) + f(t, n) case _ => 0 } val r = f(List(1, 2, 5, 10, 20, 50, 100, 200), 200) and although it runs fast enough, it's relatively inefficient, calling the f function around 5.6 million times. I saw someone else's

minimum number of steps to reduce number to 1

蹲街弑〆低调 提交于 2019-11-29 19:59:45
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? There is a pattern which allows you to know the optimal next step in constant time. In fact, there can be cases where there are two equally

Building bridges problem - how to apply longest increasing subsequence?

会有一股神秘感。 提交于 2019-11-29 19:53:42
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 to solve this problem as efficiently as possible. I have heard that this problem is related to the

how to find the number of distinct subsequences of a string?

帅比萌擦擦* 提交于 2019-11-29 19:33:04
Here is another spoj problem that asks how to find the number of distinct subsequences of a string ? For example, Input AAA ABCDEFG CODECRAFT Output 4 128 496 How can I solve this problem ? It's a classic dynamic programming problem. Let: dp[i] = number of distinct subsequences ending with a[i] sum[i] = dp[1] + dp[2] + ... + dp[i]. So sum[n] will be your answer. last[i] = last position of character i in the given string. A null string has one subsequence, so dp[0] = 1 . read a n = strlen(a) for i = 1 to n dp[i] = sum[i - 1] - sum[last[a[i]] - 1] sum[i] = sum[i - 1] + dp[i] last[a[i]] = i

Memoization Handler

浪子不回头ぞ 提交于 2019-11-29 18:25:46
问题 Is it "good practice" to create a class like the one below that can handle the memoization process for you? The benefits of memoization are so great (in some cases, like this one, where it drops from 501003 to 1507 function calls and from 1.409 to 0.006 seconds of CPU time on my computer) that it seems a class like this would be useful. However, I've read only negative comments on the usage of eval() . Is this usage of it excusable, given the flexibility this approach offers? This can save

Lazily Tying the Knot for 1 Dimensional Dynamic Programming

落花浮王杯 提交于 2019-11-29 16:58:10
问题 Several years ago I took an algorithms course where we were giving the following problem (or one like it): There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic programming write a function that will compute the number of steps it takes the elevator to get from floor i to floor j . This is obviously easy using a stateful approach, you create an array n elements long and fill it up with the values. You could even use

Counting ways to climb n steps with 1, 2, or 3 steps taken

不打扰是莪最后的温柔 提交于 2019-11-29 16:54:07
In a book I encountered following question: Given N step stair, in how many number of ways can you climb if you use either 1, 2 or 3 steps at a time? Following is the code that book has given: int countWays(int n){ if(n<0) return 0; if(n == 0) return 1; else return countWays(n-1) + countWays(n-2) + countWays(n-3); } I have the following concerns in understanding this code: I do not understand why 1 is being returned for n=0. If there are 0 steps then obviously we do not have to climb any and 0 should be returned. For n=3 function returns 4 but i can see only 3 cases i.e. (1,1,1), (1,2), (3). I

Maximum product prefix string

瘦欲@ 提交于 2019-11-29 15:40:24
问题 The following is a demo question from a coding interview site called codility: A prefix of a string S is any leading contiguous part of S. For example, "c" and "cod" are prefixes of the string "codility". For simplicity, we require prefixes to be non-empty. The product of prefix P of string S is the number of occurrences of P multiplied by the length of P. More precisely, if prefix P consists of K characters and P occurs exactly T times in S, then the product equals K * T. For example, S =