dynamic-programming

Minimum steps to set a series of rotating dials to a given sequence

假如想象 提交于 2019-12-02 21:22:59
So I have the following problem: There are n rotating dials each set to some number between 0-9 and they need to be matched with another series of n numbers(also between 0-9). One step of rotation is rotating any number of consecutive dials up or down by one step. The dials wrap around 9. i.e. rotating one step up from 9 gives 0 and vice versa. I need to find the minimum number of steps to match the initial configuration with the given configuration. Ex: Initial -> 154 Given -> 562 1. first move first 2 dials up by 1 154 -> 264 ->1 step 2. move 1st dial 3 up 264->564 ->3 steps 3. move 3rd dial

Java Programming : Dynamic Programming on stairs example

ぐ巨炮叔叔 提交于 2019-12-02 21:20:24
A man is running up a staircase with n steps, and can go either 1 steps, 2 steps, or 3 steps at a time. Now write a program to count how many possible ways the child can run the stairs. The code given is like below public static int countDP(int n, int[] map) { if (n<0) return 0; else if (n==0) return 1; else if (map[n]>-1) return map[n]; else { map[n] = countDP(n-1, map) + countDP(n-2, map) + countDP(n-3, map); return map[n]; } } I know C and C++, not JAVA. This is from the Cracking the Coding interview book. Could anybody can explain why and how she employs the function map here? map here is

Knapsack with continuous (non distinct) constraint

做~自己de王妃 提交于 2019-12-02 21:09:18
I watched Dynamic Programming - Kapsack Problem (YouTube) . However, I am solving a slightly different problem where the constraint is the budget, price, in double, not integer. So I am wondering how can I modify that? Double is "continuous" unlike integer where I can have 1,2,3 .... I don't suppose I do 0.0, 0.1, 0.2 ...? UPDATE 1 I thought of converting double to int by multiply by 100. Money is only 2 decimal places. But that will mean the range of values will be very large? UPDATE 2 The problem I need to solve is: Items have a price (double) & satisfaction (integer) value. I have a budget

What is the “cut-and-paste” proof technique?

…衆ロ難τιáo~ 提交于 2019-12-02 20:16:57
I've seen references to cut-and-paste proofs in certain texts on algorithms analysis and design. It is often mentioned within the context of Dynamic Programming when proving optimal substructure for an optimization problem (See Chapter 15.3 CLRS). It also shows up on graphs manipulation. What is the main idea of such proofs? How do I go about using them to prove the correctness of an algorithm or the convenience of a particular approach? The term "cut and paste" shows up in algorithms sometimes when doing dynamic programming (and other things too, but that is where I first saw it). The idea is

Two player grid traversal game

老子叫甜甜 提交于 2019-12-02 19:46:38
Given a M * N grid and location of two players p1 and p2 on grid. There are n balls placed on different positions on the grid. Let the location of these balls be B(1), B(2), B(3) ..., B(n) . We need to calculate the minumum manhattan distance required to pick all the balls. Balls should be picked in ascending order i.e if B(i) is picked before B(j) if i < j . Consider the following sample case: p1 = (1, 1) p2 = (3, 4) Lets consider location of balls as B(1) = (1, 1), B(2) = (2, 1), B(3) = (3, 1), B(4) = (5, 5) Output will be 5 because p1 will first choose B(1), B(2), B(3) and p1 will choose B

Finding the Longest Palindrome Subsequence with less memory

ε祈祈猫儿з 提交于 2019-12-02 19:35:49
I am trying to solve a dynamic programming problem from Cormem's Introduction to Algorithms 3rd edition (pg 405) which asks the following: A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic , racecar , and aibohphobia (fear of palindromes). Give an efficient algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input character , your algorithm should return carac . Well, I could solve it in two ways: First solution: The Longest

Dynamic programming - Coin change decision

扶醉桌前 提交于 2019-12-02 18:47:32
I'm reviewing some old notes from my algorithms course and the dynamic programming problems are seeming a bit tricky to me. I have a problem where we have an unlimited supply of coins, with some denominations x1, x2, ... xn and we want to make change for some value X. We are trying to design a dynamic program to decide whether change for X can be made or not (not minimizing the number of coins, or returning which coins, just true or false). I've done some thinking about this problem, and I can see a recursive method of doing this where it's something like... MakeChange(X, x[1..n this is the

Memory-constrained coin changing for numbers up to one billion

房东的猫 提交于 2019-12-02 18:08:25
I faced this problem on one training. Namely we have given N different values ( N<= 100 ). Let's name this array A[N] , for this array A we are sure that we have 1 in the array and A[i] ≤ 10 9 . Secondly we have given number S where S ≤ 10 9 . Now we have to solve classic coin problem with this values. Actually we need to find minimum number of element which will sum to exactly S . Every element from A can be used infinite number of times. Time limit: 1 sec Memory limit: 256 MB Example: S = 1000, N = 10 A[] = {1,12,123,4,5,678,7,8,9,10}. The result is 10. 1000 = 678 + 123 + 123 + 12 + 12 + 12

Efficient table for Dynamic Programming in Haskell

≡放荡痞女 提交于 2019-12-02 17:46:46
I've coded up the 0-1 Knapsack problem in Haskell. I'm fairly proud about the laziness and level of generality achieved so far. I start by providing functions for creating and dealing with a lazy 2d matrix. mkList f = map f [0..] mkTable f = mkList (\i -> mkList (\j -> f i j)) tableIndex table i j = table !! i !! j I then make a specific table for a given knapsack problem knapsackTable = mkTable f where f 0 _ = 0 f _ 0 = 0 f i j | ws!!i > j = leaveI | otherwise = max takeI leaveI where takeI = tableIndex knapsackTable (i-1) (j-(ws!!i)) + vs!!i leaveI = tableIndex knapsackTable (i-1) j --

Total number of palindromic subsequences in a string

岁酱吖の 提交于 2019-12-02 17:21:43
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 dynamic programming is not even required. Suggestions please. And there is one more catch. When the