dynamic-programming

Total number of palindromic subsequences in a string

杀马特。学长 韩版系。学妹 提交于 2019-12-03 03:58:54
问题 The question is like this-- For every string given as input, you need to tell the number of subsequences of it that are palindromes (need not necessarily be distinct). Note that the empty string is not a palindrome. For example, the palindromic subsequences of "aab" are: "a", "a", "b", "aa", and the method returns 4. I had the Dynamic Programming solution to finding Longest Palindromic Subsequence in mind and therefore tried to take ideas from it. Couldn't really get the solution. May be

find a solution to subset sum using dynamic programming

家住魔仙堡 提交于 2019-12-03 03:45:48
What I want to do I want to find a subset of an array that sums to a target T . I also want to use to a dynamic programming approach (and a bottom-up solution at that) to do this. What I currently have Currently I only found a way to see if amongst all subsets of size N , whether or not there is at least one subset that has the desired sum. See code below. public boolean solve(int[] numbers, int target) { //Safeguard against invalid parameters if ((target < 0) || (sum(numbers) < target)){ return false; } boolean [][] table = new boolean [target + 1] [numbers.length + 1] ; for (int i = 0; i <=

Is “house coloring with three colors” NP?

瘦欲@ 提交于 2019-12-03 03:41:10
Consider the problem described here (reproduced below.) Can some better known NP-complete problem be reduced to it? The problem: There are a row of houses. Each house can be painted with three colors: red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with minimum cost. How would you do it? Note: The cost of painting house 1 red is different from that of painting house 2 red. Each combination of house and color has its own cost. Knoothe No, it

Dynamic programming exercise for string cutting

痴心易碎 提交于 2019-12-03 03:04:47
I have been working on the following problem from this book . A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20-character string at positions 3 and 10, then making the first cut at position 3 incurs a total cost of 20+17

given an array of integers in random order you have to find the minimum number of swaps to convert it to cyclic sorted array

一曲冷凌霜 提交于 2019-12-03 03:01:44
if an array is given in random order , you have to output the minimum number of swaps required to convert into cyclic sorted array. e.g. array given is 3 5 4 2 1 so the first swap will be 5<-->4 result : 3 4 5 2 1 second swap will be 2<-->1 result : 3 4 5 1 2 (final) output : 2 i am not able to get the logic behind this problem. adding some more : swap only possible between adjacent elements and numbers are between range 1 to N Well, don't know if it is the best algorithm available, but I can think of a O(n^2) solution: First, ignore the possibility of the cyclic array. Let's solve a simpler

Convert EntityReference to Entity

空扰寡人 提交于 2019-12-03 02:53:08
Does anyone know how can Convert EntityReference to Entity. protected override void Execute(CodeActivityContext executionContext) { [Input("Email")] [ReferenceTarget("email")] public InArgument<Entity> EMail { get; set; } Entity MyEmail = EMail.Get<Entity>(executionContext); This give me an error. Cannot convert this. The shortest answer to your questions is to query the database for the entity that's pointed out (referred to) by the entity reference. I've always viewed entity references as (rough) equivalent to the pointers in C++. It's got the address to it (guid) but you need to de

Discover long patterns

落爺英雄遲暮 提交于 2019-12-03 02:18:07
问题 Given a sorted list of numbers, I would like to find the longest subsequence where the differences between successive elements are geometrically increasing. So if the list is 1, 2, 3, 4, 7, 15, 27, 30, 31, 81 then the subsequence is 1, 3, 7, 15, 31 . Alternatively consider 1, 2, 5, 6, 11, 15, 23, 41, 47 which has subsequence 5, 11, 23, 47 with a = 3 and k = 2. Can this be solved in O( n 2 ) time? Where n is the length of the list. I am interested both in the general case where the progression

Algorithm that balances the number of elements in a subinterval of an array?

耗尽温柔 提交于 2019-12-03 00:48:01
Lets say you have an array with 4 different types of elements. 1 1 2 3 1 2 2 3 3 4 4 1. I want to find the longest subinterval that results in an equal number of each elements and the largest total number of elements. In this case, it would be 1 1 2 3 1 2 2 3 3 because this results in 3 twos, 3 threes, and 3 ones. I believe this is some sort of modified dynamic programming, or something that requires prefix sums but I am not too sure. Can someone give me insight on how to start? #!/usr/bin/env python #The demo data SET = [1,1,2,3,1,2,2,3,3,4,4,1] #Function to map the counts of the values in

What's the difference between recursion, memoization & dynamic programming? [duplicate]

匆匆过客 提交于 2019-12-03 00:12:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Dynamic programming and memoization: top-down vs bottom-up approaches I have gone through a lot of articles on this but can't seem to make sense of it. At times recursion and dynamic programming looks the same and at others memoization & dynamic programming look alike. Can someone explain to me what's the difference? P.S. It will also be helpful if you could point me to some code using the three approaches on

8-queen problem using Dynamic programming

怎甘沉沦 提交于 2019-12-02 23:53:52
I am quite confused with idea of implementing 8-queen problem using dynamic programming. It seems it is not possible at one end as for DP " if the problem was broken up into a series of subproblems and the optimal solution for each subproblem was found, then the resulting solution would be realized through the solution to these subproblems. A problem that does not have this structure cannot be solved with dynamic programming" ( Reference ). By taking this in account, the optimal solution for 7x7 board might not optimal as well (even incorrect) for 8x8. So, the result of problem might not