dynamic-programming

Is partitioning an array into halves with equal sums P or NP?

南笙酒味 提交于 2019-11-27 03:39:54
问题 This was an algorithm interview question about the Partition Problem. You are given an array which consists of numbers with between 0 and 5 digits. Write a function which will return whether the array can be divided into 2 halves such a that sum of the two halves would be equal. Is this an NP problem or can it be solved by dynamic programming? "between 0 and 5 digit" means 0 ~ 99999, I think. I found a good answer to this outside SO here. 回答1: Both. The problem is in NP, and I'm pretty sure

Maximise sum of “non-overlapping” numbers from matrix

牧云@^-^@ 提交于 2019-11-27 03:37:17
问题 Just looking for a bit of direction, I realise that the example given is possible to solve using brute force iteration, but I am looking for a more elegant (ie. mathematical?) solution which could potentially solve significantly larger examples (say 20x20 or 30x30). It is entirely possible that this cannot be done, and I have had very little success in coming up with an approach which does not rely on brute force... I have a matrix (call it A) which is nxn. I wish to select a subset (call it

Maximize consumption Energy

一笑奈何 提交于 2019-11-27 03:23:21
问题 There are three types of foods were provided i.e. meat, cake and pizza and N different stores selling it where, i can only pick one type of food from each store . Also I can only buy items in A, B and C numbers where 'A' means, Meat from total 'A' number of different stores (see example). My task is to consume food, so that i can have maximum amount of energy. example, 10 <= number of stores <br> 5 3 2 <= out of 10 stores I can pick meat from 5 stores only. Similarly, I can pick cake from 3

How to split a string into words. Ex: “stringintowords” -> “String Into Words”?

痞子三分冷 提交于 2019-11-27 03:04:19
What is the right way to split a string into words ? (string doesn't contain any spaces or punctuation marks) For example: "stringintowords" -> "String Into Words" Could you please advise what algorithm should be used here ? ! Update: For those who think this question is just for curiosity. This algorithm could be used to camеlcase domain names ("sportandfishing .com" -> "SportAndFishing .com") and this algo is currently used by aboutus dot org to do this conversion dynamically. As mentioned by many people here, this is a standard, easy dynamic programming problem: the best solution is given

how to find longest palindromic subsequence?

浪尽此生 提交于 2019-11-27 02:54:19
Here is the problem (6.7 ch6 ) from Algorithms book (by Vazirani) that slightly differs from the classical problem that finding longest palindrome . How can I solve this problem ? A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Devise an algorithm that takes a sequence x[1 ...n] and returns the (length of the) longest palindromic subsequence. Its running time should be

Dynamic Programming Optimal Coin Change

╄→尐↘猪︶ㄣ 提交于 2019-11-27 02:53:03
问题 I've been reviewing some dynamic programming problems, and I have had hard time wrapping my head around some code in regards to finding the smallest number of coins to make change. Say we have coins worth 25, 10, and 1, and we are making change for 30. Greedy would return 25 and 5(1) while the optimal solution would return 3(10). Here is the code from the book on this problem: def dpMakeChange(coinValueList,change,minCoins): for cents in range(change+1): coinCount = cents for j in [c for c in

algorithm to find longest non-overlapping sequences

爱⌒轻易说出口 提交于 2019-11-27 01:41:29
问题 I am trying to find the best way to solve the following problem. By best way I mean less complex. As an input a list of tuples (start,length) such: [(0,5),(0,1),(1,9),(5,5),(5,7),(10,1)] Each element represets a sequence by its start and length , for example (5,7) is equivalent to the sequence (5,6,7,8,9,10,11) - a list of 7 elements starting with 5. One can assume that the tuples are sorted by the start element. The output should return a non-overlapping combination of tuples that represent

3-PARTITION problem

谁说我不能喝 提交于 2019-11-27 01:25:18
问题 here is another dynamic programming question (Vazirani ch6) Consider the following 3-PARTITION problem. Given integers a1...an, we want to determine whether it is possible to partition of {1...n} into three disjoint subsets I, J, K such that sum(I) = sum(J) = sum(K) = 1/3*sum(ALL) For example, for input (1; 2; 3; 4; 4; 5; 8) the answer is yes, because there is the partition (1; 8), (4; 5), (2; 3; 4). On the other hand, for input (2; 2; 3; 5) the answer is no. Devise and analyze a dynamic

Optimal way of filling 2 knapsacks?

一笑奈何 提交于 2019-11-27 01:09:12
问题 The dynamic programming algorithm to optimally fill a knapsack works well in the case of one knapsack. But is there an efficient known algorithm that will optimally fill 2 knapsacks (capacities can be unequal)? I have tried the following two approaches and neither of them is correct. First fill the first knapsack using the original DP algorithm to fill one knapsack and then fill the other knapsack. First fill a knapsack of size W1 + W2 and then split the solution into two solutions (where W1

Sum of digits of a factorial

帅比萌擦擦* 提交于 2019-11-26 23:57:54
问题 Link to the original problem It's not a homework question. I just thought that someone might know a real solution to this problem. I was on a programming contest back in 2004, and there was this problem: Given n, find sum of digits of n!. n can be from 0 to 10000. Time limit: 1 second. I think there was up to 100 numbers for each test set. My solution was pretty fast but not fast enough, so I just let it run for some time. It built an array of pre-calculated values which I could use in my