dynamic-programming

Algorithm to find maximum sum of elements in an array such that not more than k elements are adjacent

我的未来我决定 提交于 2019-12-30 06:21:08
问题 I came across this question. Given an array containing only positive values, you want to maximize the sum of chosen elements under the constraint that no group of more than k chosen elements are adjacent. For example if input is 1 2 3 1 7 9 (n=6 and k =2). The output will be 21, which comes from picking the elements _ 2 3 _ 7 9. My simple DP solution is this #include<stdio.h> #include<limits.h> #include<malloc.h> long maxsum(int n,int k,long *sums){ long *maxsums; maxsums = malloc(sizeof(long

Find subset of size k such that the minimum distance between values is maximum

冷暖自知 提交于 2019-12-30 03:31:06
问题 Suppose i have an array which contain n integers . How to find subset of size k such that the minimum distance between all pairs of integers in the subset is maximized , i mean they are at farthest distance . example : array a[]={1,2,6,7,10} and k=3 , subset = {1,6,10} , the minimum distance is 4 between 10 and 6 . Wrong subsets : {1,7,10} , minimum distance is 3 {1,2,6} , minimum distance is 1 I came up with a solution : 1) sort array 2) select a[0] , now find ceil(a[0]+ x ) = Y in array ...

How do you check if one array is a subsequence of another?

假如想象 提交于 2019-12-29 08:57:11
问题 I'm looking to explore different algorithms, both recursive and dynamic programming, that checks if one arrayA is a subsequence of arrayB. For example, arrayA = [1, 2, 3] arrayB = [5, 6, 1, 7, 2, 9, 3] thus, arrayA is indeed a subsequence of arrayB. I've tried a few different searches, but all I can seem to find is algorithms to compute the longest increasing subsequence. 回答1: Since you must match all elements of arrayA to some elements of arrayB , you never need to backtrack . In other words

Find all triplets in array with sum less than or equal to given sum

守給你的承諾、 提交于 2019-12-29 04:45:08
问题 This was recently asked to a friend in an interview and we do not know of any solution other than the simple O(n 3 ) one. Is there some better algorithm? The question is to find all triplets in an integer array whose sum is less than or equal to given sum S. Note: I have seen other such problems on SO with performance O(n 2 log n) but all of them were solving the easier version of this problem like where arr[i] + arr[j] + arr[k] = S or where they were only checking whether one such triplet

Coin change with limited number of coins

老子叫甜甜 提交于 2019-12-29 01:37:31
问题 I have written a program for generating subset sum which might be used in this problem which states: Suppose, you have 3 $1-coins, 2 $2-coins, 3 $5-coins, 1 $10-coin, there are 4 ways to obtain $10 from those coins. If there are n1 $X1 coins, n2 $X2 coins.... nm $Xm coins, how many ways can we obtain $X from these limited number of coins? If we create a set of { X1, X1..... X1, X2, X2.......... X2, ..., ..., ............, Xm, Xm... Xm}, and then run Subset summing on it, surely we can get a

Dynamic Programming Coin Change Problems

陌路散爱 提交于 2019-12-28 16:07:13
问题 I am having issues with understanding dynamic programming solutions to various problems, specifically the coin change problem: "Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are

Is there a generic way to memoize in Scala?

寵の児 提交于 2019-12-28 02:29:29
问题 I wanted to memoize this: def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2) println(fib(100)) // times out So I wrote this and this surprisingly compiles and works (I am surprised because fib references itself in its declaration): case class Memo[A,B](f: A => B) extends (A => B) { private val cache = mutable.Map.empty[A, B] def apply(x: A) = cache getOrElseUpdate (x, f(x)) } val fib: Memo[Int, BigInt] = Memo { case 0 => 0 case 1 => 1 case n => fib(n-1) + fib(n-2) } println(fib(100)) //

Dynamic LINQ to Entities, how to build query based on variables

北城以北 提交于 2019-12-25 17:44:46
问题 The query I need to build is this: query = query.Where(s => ( (s.Title.Contains(title1) && s.EpisodeTitle.Contains(episodeTitle1)) || (s.Title.Contains(title2) && s.EpisodeTitle.Contains(episodeTitle2))) ); The only issue is, s.Title and s.EpisodeTitle are dynamic. Meaning that the following variables could be part of the query: (string title1 = null, string title2 = null, string episodeTitle1 = null, string episodeTitle2 = null, string genre = null, string directorName = null, string

parse text into valid sentence

穿精又带淫゛_ 提交于 2019-12-25 14:29:14
问题 I have a doubt about how to parse any text into valid sentence. Suppose a text is given iamjhamb and parse into i am jhamb My approach: I solved this using Dynamic programmnig, Make an array T[], where T[i] shows string from 0 to i made any valid setence or not formula is T[i] = 1 iff T[j] = 1 and substring(j+1, i) is a word in dictionary for all j < i. But this approach is not totally correct, it gives all possible words form from this text, as this is not the demand of this questioin. So,

Subset sum solution length

左心房为你撑大大i 提交于 2019-12-25 04:36:22
问题 I'm using the following logic to solve the subset sum problem as described in this question: Total sum from a set (logic). It is working and it will give me one random solution every time, the problem is that I need only the solutions with an specific amount of items. For example: [2,3,4,6,3] //Set of values SUM = 6 The current solutions I can get are: [4,2] [6] [3,3] But what if I need this method to randomly return only a solution with (for example) 2 items? 回答1: Just in case somebody needs