dynamic-programming

Longest subsequence that first increases then decreases

好久不见. 提交于 2019-12-05 09:45:42
I am trying to solve the following question : A sequence in which the value of elements first decrease and then increase is called V- Sequence. In a valid V-Sequence there should be at least one element in the decreasing and at least one element in the increasing arm. For example, "5 3 1 9 17 23" is a valid V-Sequence having two elements in the decreasing arm namely 5 and 3, and 3 elements in the increasing arm namely 9, 17 and 23 . But none of the sequence "6 4 2" or "8 10 15" are V-Sequence since "6 4 2" has no element in the increasing part while "8 10 15" has no element in the decreasing

Coin Change : Greedy Approach

南笙酒味 提交于 2019-12-05 08:02:52
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 a set of denominations, how can we say whether greedy approach creates an optimal solution? What you

Clarification of Answer… find the max possible two equal sum in a SET

放肆的年华 提交于 2019-12-05 07:05:24
I need a clarification of the answer of this question but I can not comment (not enough rep) so I ask a new question. Hope it is ok. The problem is this: Given an array, you have to find the max possible two equal sum, you can exclude elements. i.e 1,2,3,4,6 is given array we can have max two equal sum as 6+2 = 4+3+1 i.e 4,10,18, 22, we can get two equal sum as 18+4 = 22 what would be your approach to solve this problem apart from brute force to find all computation and checking two possible equal sum? edit 1: max no of array elements are N <= 50 and each element can be up to 1<= K <=1000 edit

Distribution of balls into 'bins with given capacities' using Dynamic Programming

痴心易碎 提交于 2019-12-05 06:40:39
I was wondering how to solve such a problem using DP. Given n balls and m bins, each bin having max. capacity c1, c2,...cm. What is the total number of ways of distributing these n balls into these m bins. The problem I face is How to find a recurrence relation (I could when the capacities were all a single constant c). There will be several test cases, each having its own set of c1,c2....cm. So how would the DP actually apply for all these test cases because the answer explicitly depends on present c1,c2....cm, and I can't store (or pre-compute) the answer for all combinations of c1,c2....cm.

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

此生再无相见时 提交于 2019-12-05 06:31:50
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 You can solve this by using dynamic programming. Check this for a possible solution. Just an explicit solution to the equations given implicitly in taskinoor's answer: Or f[n]=((1 + (-1)^n)*((2 - Sqrt[3])^(n/2)*(-1 + Sqrt[3]) + (1 + Sqrt[3])* (2 + Sqrt[3])^(n/2)))/(4*Sqrt[3]) if anyone

Recursion on staircase

一笑奈何 提交于 2019-12-05 05:21:47
问题 I'm trying to understand the solution provided in a book to the following question: "A child is running up a staircase with n steps and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs." The book's solution is as follows, stemming from the fact that "the last move may be a single step hop from n - 1, a double step hop from step n - 2 or a triple step hop from step n - 3" public static int countWaysDP(int n,

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

自古美人都是妖i 提交于 2019-12-05 04:33:19
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 the 0-1 knapsack, although 2D Tough homework problem. This is essentially the optimization version of

Most effecient way to compute a series of moves in peg solitaire

半城伤御伤魂 提交于 2019-12-05 04:21:16
问题 Given an arbitary peg solitaire board configuration, what is the most effecient way to compute any series of moves that results in the "end game" position. For example, the standard starting position is: ..***.. ..***.. ******* ***O*** ******* ..***.. ..***.. And the "end game" position is: ..OOO.. ..OOO.. OOOOOOO OOO*OOO OOOOOOO ..OOO.. ..OOO.. Peg solitare is described in more detail here: Wikipedia, we are considering the "english board" variant. I'm pretty sure that it is possible to

Cutting algorithm of two dimensional board

陌路散爱 提交于 2019-12-05 02:30:48
问题 I have problem with my homework. Given a board of dimensions m x n is given, cut this board into rectangular pieces with the best total price. A matrix gives the price for each possible board size up through the original, uncut board. Consider a 2 x 2 board with the price matrix: 3 4 3 6 We have a constant cost for each cutting for example 1 . Piece of length 1 x 1 is worth 3 . Horizontal piece of length 1 x 2 is worth 4 . Vertical piece of length 1 x 2 is worth 3 . Whole board is worth 6 .

Algorithm for distributing beads puzzle (2)?

只谈情不闲聊 提交于 2019-12-05 01:43:27
问题 Let's say you have a circle (shown below) with N slots. Your goal is to end up with a specified number of beads in each slot, and you have an array of size N containing the amount of beads you need in each slot. For example, if the array was {1, 5, 3}, then you would need to end up with 1 bead in slot 1, 5 beads in slot 2, and 3 beads in slot 3. You have an infinite amount of beads. You can "unlock" X slots. Once you unlock a slot, you can start putting beads in that slot. You can move beads