dynamic-programming

Count all possible strings from given mobile numeric keypad sequence

∥☆過路亽.° 提交于 2019-12-11 19:49:29
问题 How to count all possible string that can be formed from a given sequence of digits(from 2-9) where each digit represents a mobile button and is mapped to 3/4 alphabet. eg:- 2 is mapped to A,B,C, by pressing the button 2 three times "222", possible strings that can be formed are {"AAA","AB","BA","C"}. input= "2233", possible strings={"AADD","AAE","BDD","BE"}. I need a pseudo-code to implement the above problem. 回答1: Algorithm/Intuition: As it represents in the above image, for a single digit,

Subset Sum : Find subset whose sum greater than K

余生颓废 提交于 2019-12-11 18:37:11
问题 I have an array of positive integers say {a1, a2, ...., an}, and I want to find out all possible subsets of the array which satisfy the following condition: (sum >= K) where K is a positive integer. I know the solution for this problem is dynamic programming, but unable to think how to use it for this case. Please help. P.S. : I don't exactly need all the subsets, but say product of all elements for all subsets formed. 回答1: Your problem looks similar to 0-1 Knapsack problem, except on thing -

How to use Dynamic Programming number of distinct function call is too large as compare to the maximum allowed size of the array

老子叫甜甜 提交于 2019-12-11 18:34:57
问题 I was giving some programming contest.............. and I have solved one problem related to the dynamic programming.Below is the link of the problem Prblem link I have given the solution of the problem as follows-: #include<stdio.h> short int moves[10000000]; int minimum(int a ,int b, int c) { if(a<b) if(a<c) return a; else return c; else if(b<c) return b; else return c; } int FindMoves(int strength) { int m1=0,m2=0,m3=0; int isBy2=0,isBy3=0; if(strength==1) { moves[1]=0; return 0; } else if

Subset sum Swift

可紊 提交于 2019-12-11 13:48:31
问题 I want to use dynamic programming to get the sum of all subsets from a given array. subsets([1,2,3]) = 24 Since the subsets are {1}{2}{3}{1,2}{2,3}{1,3}{1,2,3} I'm trying to calculate subsets([1,2]) which = 6 My implementation below gets 5 func subsets(_ arr: [Int]) -> Int { var mem = Array(repeating: 0, count: arr.count) return subsetsRecursive(arr, 0, &mem) } // correct result, but too slow so requires memorization func subsetsRecursive(_ arr: [Int], _ ptr: Int, _ memo : inout [Int]) -> Int

Coin change - DP

无人久伴 提交于 2019-12-11 13:38:15
问题 I have a small problem understanding the coin change problem in dynamic programming. Simply put, I have to change a sum using a minimum number of coins. I have n denominations of coins of values 1 = v1 < v2 < ... < vn, and we note M(j) the minimum number of coins required to make change for amount j. In the above formula I don't understand what M(j-vi) means. vi has to be the maximum value of the coins used in j-1? 回答1: You're making piles of coins for different values j, named M(j). The

How to make fromList lazy in this dynamic programming example?

社会主义新天地 提交于 2019-12-11 13:37:29
问题 module Main where import System.Random import Data.Foldable import Control.Monad import qualified Data.Map as M import qualified Data.Vector as V import Debug.Trace import Data.Maybe import Data.Ord -- Represents the maximal integer. maxBound is no good because it overflows. -- Ideally should be something like a billion. maxi = 1000 candies :: V.Vector Int -> Int --M.Map (Int, Int) Int candies ar = ff [l (V.length ar - 1) x | x <- [0..maxi]] where go :: Int -> Int -> Int go _ 0 = maxi go 0 j

Minimum cost subset of sensors covering targets

血红的双手。 提交于 2019-12-11 10:13:22
问题 I have a question in dynamic programming, If I have a set of sensors covering targets ( a target might be covered by mutiple sensors) how can I find the minimum cost subset of sensors knowing that each sensors has its own cost? I thought a lot about this, but I cant reach the recursive forumla to write my program? greedy algorithm gives me wrong minimum cost subset sometimes, and my problem is that sensors overlap in covering targets, any help? For Example: I have set of sensors with cost

Coin Change :Dynamic Programming

依然范特西╮ 提交于 2019-12-11 09:59:07
问题 The code I have written solves the basic coin change problem using dynamic programming and gives the minimum number of coins required to make the change. But I want to store the count of each coin playing part in the minimum number. What I am trying to do is initializing an array count[] and just like hashing it increments the number of coin[j] whenever min is found, i.e count[coin[j]]++ . But this is not working the way I wanted because it adds the coin every time it finds min corresponding

Regarding approach to solving sliding tiles puzzle

混江龙づ霸主 提交于 2019-12-11 08:59:51
问题 I have started reading "Think Like A Programmer" by V Anton Spraul. Here is the question. The train technique mentioned in the book works fine for the example sighted in it. I was attempting to write the train approach method to solve the sliding tiles problem. Assuming that I am working on subset of the complete problem, for the below set of tiles (as given as example in the book), the approach mentioned works fine. 6 8 . 5 4 7 We move anti-clock wise until we get 4,5,6 in order in top row

Reducing time complexity of Knapsack 0~1 while using DP algorithm

不羁岁月 提交于 2019-12-11 08:05:46
问题 I'm using DP algorithm, i.e. storing sub-problem values in 2D array where one axis means n items and other - w values from 0 to W where W is the maximum capacity of knapsack. Therefore T[n-1][W] value is the optimum I need to calculate. I've read in other sources that time complexity of this algorithm is O(nW) . My quesiton would be: is it possible to reduce this time complexity even more? I found other answer which talks about pretty much same thing but I can't understant it without example: