dynamic-programming

In how many ways can you tile a 3xn rectangle with 2x1 dominoes?

时光怂恿深爱的人放手 提交于 2019-12-07 02:08:47
问题 Everyday I struggle with algorithm questions and try to ask here which I can't answer. Excuse me, if I cause any headache. Anyway, Here is the problem from the University of Waterloo ACM Programming Contest. In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Nirvana : smells like recursion spirit 回答1: You can solve this by using dynamic programming. Check this for a possible solution. 回答2: Just an explicit solution to the equations given implicitly in taskinoor's answer: Or f[n]

Coin Change : Greedy Approach

☆樱花仙子☆ 提交于 2019-12-07 01:52:21
问题 The Problem is making n cents change with quarters, dimes, nickels, and pennies, and using the least total number of coins. In the particular case where the four denominations are quarters,dimes, nickels, and pennies, we have c1 = 25, c2 = 10, c3 = 5, and c4 = 1. If we have only quarters, dimes, and pennies (and no nickels) to use, the greedy algorithm would make change for 30 cents using six coins —a quarter and five pennies—whereas we could have used three coins , namely, three dimes. Given

What is an algorithm to split a group of items into 3 separate groups fairly?

痞子三分冷 提交于 2019-12-07 00:37:37
问题 I have this problem in my textbook: Given a group of n items, each with a distinct value V(i), what is the best way to divide the items into 3 groups so the group with the highest value is minimIzed? Give the value of this largest group. I know how to do the 2 pile variant of this problem: it just requires running the knapsack algorithm backwards on the problem. However, I am pretty puzzled as how to solve this problem. Could anyone give me any pointers? Answer: Pretty much the same thing as

Longest `subsequence` of balanced parentheses

折月煮酒 提交于 2019-12-06 19:56:27
I am trying to solve a variant of a problem I asked earlier: Given a string of parentheses (length <= 1,000,000) and a list of range queries, find the longest subsequence of balanced parentheses within each of the ranges for each of the <= 100,000 queries I found this other SO question that is similar but only has an O(N^3) algorithm. I believe that a DP solution of the form dp[i, j] = longest balanced subsequence in [i .. j] should work because once computed, this would enable to to answer all of the range queries just by querying the DP table. However, even an O(N^2) solution to this problem

Polymorphic Model Bindable Expression Trees Resolver

自古美人都是妖i 提交于 2019-12-06 19:05:56
问题 I'm trying to figure out a way to structure my data so that it is model bindable. My Issue is that I have to create a query filter which can represent multiple expressions in data. For example: x => (x.someProperty == true && x.someOtherProperty == false) || x.UserId == 2 x => (x.someProperty && x.anotherProperty) || (x.userId == 3 && x.userIsActive) I've created this structure which represents all of the expressions fine my Issue is how can I make this so it's property Model Bindable public

How to find the minimum number of operation(s) to make the string balanced?

怎甘沉沦 提交于 2019-12-06 16:55:23
From Codechef : A string is considered balanced if and only if all the characters occur in it equal number of times. You are given a string S ; this string may only contain uppercase English letters. You may perform the following operation any number of times (including zero): Choose one letter in S and replace it by another uppercase English letter. Note that even if the replaced letter occurs in S multiple times, only the chosen occurrence of this letter is replaced. Find the minimum number of operations required to convert the given string to a balanced string. Example: For input: ABCB Here

Dynamic programming doesn't give correct answer

僤鯓⒐⒋嵵緔 提交于 2019-12-06 16:42:16
I recently found out about the technique called dynamic programming and I stumbled upon a problem which I can't figure out. You are given a list of arguments in the beginning and you need to do sums on as if you were cutting it. If the list has only one element, you don't sum it. If it has more, you sum the elements and cut it in every possible way. So if list has n elements, there are just n-1 ways to cut it. The picture will explain: I first wanted to sum up all of the sumable parts and I expected the result 20( 11 + 9 ) ( even thought the correct answer is 9 ), but I thought it would be a

Find all combinations of a given set of integers summing up to a given sum

≯℡__Kan透↙ 提交于 2019-12-06 15:36:28
问题 I am looking for an answer to the following problem. Given a set of integers (no duplicates) and a sum, find all possible combinations of the set's elements summing up to the sum. Solutions order does not matter (solutions {2, 2, 3} and {3, 2 ,2} are equal). Please note that the final combination does not need to be a set, as it can contain duplicates. Example: Set {2,3,5} Sum 10 Result: {2, 2, 2, 2, 2}, {2, 2, 3, 3}, {2, 3, 5}, {5, 5} I've looked at Subset Sum problem as well as Coin Change

Dynamic Programming - Primitive Calculator Python [duplicate]

喜夏-厌秋 提交于 2019-12-06 15:32:11
This question already has an answer here : Dynamic programming for primitive calculator (1 answer) Closed 3 years ago . This assignment aims to implement a dynamic programming approach to a primitive calculator that can only add 1, multiply by 2 and multiply by 3. So with an input of n determine the minimum number of operations to reach n. I've implemented a very naive dp or what I think is a dp approach. It is not working. I have no-one else to ask. For an input of n = 5 the output of the below is: ([0, 1, 2, 2, 3, 4], [1, 1, 2, 3, 4, 5]) whereas there are two correct outputs for the list

Special Pairs with sum as Prime Number

徘徊边缘 提交于 2019-12-06 14:08:53
A number N is given in the range 1 <= N <= 10^50 . A function F(x) is defined as the sum of all digits of a number x. We have to find the count of number of special pairs (x, y) such that: 1. 0 <= x, y <= N 2. F(x) + F(y) is prime in nature We have to count (x, y) and (y, x) only once. Print the output modulo 1000000000 + 7 My approach: Since the maximum value of sum of digits in given range can be 450 (If all the characters are 9 in a number of length 50, which gives 9*50 = 450 ). So, we can create a 2-D array of size 451*451 and for all pair we can store whether it is prime or not. Now, the