dynamic-programming

algorithm for the 0-1 Knapsack with 2 sacks?

血红的双手。 提交于 2019-12-25 04:26:53
问题 formally, say, we have 2 sacks with capacities c1 and c2. There are N items with profits pi and weights wi. As in 0-1 Knapsack problem, we need to fill in c1 and c2 with these items in such a way the overall profit is maximized. Assume pi and wi are positive integers! For the 2 knapsack problem does below recurrence relation hold good? DP[I][J][K] is maximum profit we could achieve from the first i items such that the weight of exactly j was used in knapsack #1 and a weight of exactly k was

Dynamic Programming Issue - Fibonacci Sequence

久未见 提交于 2019-12-25 03:05:05
问题 I was reading this Wikipedia article, and attempting to implement a 'map' based solution in C, where a 'map' is just an int array, initialized to 0. For some reason it works up to fib(93) , then starts outputting strange numbers. If it matter's I'm specifying -std=c99 : #include <stdio.h> #include <stdlib.h> // represents a fib num typedef unsigned long long fib_t; // the default value of our 'map' const int FIB_NULL = 0; // could get from input, set here for now const int FIB_MAX = 100; //

Leetcode Best Time to Buy and Sell Stock with Transaction Fee, How to think in it

六眼飞鱼酱① 提交于 2019-12-25 01:45:28
问题 I understand the solution to Best Time to Buy and Sell Stock with Transaction Fee, and other 5 problems relative to Stock Sell. I just want deep understanding of how to come up with such recursive relation in problems alike. I have read https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/most-consistent-ways-of-dealing-with-the-series-of-stock-problems and I understand entirely. But, I am having trouble resolving some similar problem with minor

Find minimum possible height of the highest tower

妖精的绣舞 提交于 2019-12-24 23:15:56
问题 I have towers with height H = h1,h2,h3... and i have a cutting machine that only cuts each tower with a specific length A = a1,a2,a3.. and i have m cuts find the minimum possible height of the highest tower. for ex- H = 1, 4, 9, 16, 25 and A = 1,2,3,4,5 m = 3 ( only 3 cuts ) the minimum possible height is 15 as after cuts the array looks like H = 1,4,9,12,15 ( after applying 0, 0 , 0, 1, 2 cuts respectively on towers) What I tried : I recognised it as greedy problem (correct me if i am wrong)

Dynamic Programming: Longest Common Subsequence

丶灬走出姿态 提交于 2019-12-24 14:49:21
问题 I'm going over notes that discuss dynamic programming in the context of finding the longest common subsequence of two equal-length strings. The algorithm in question outputs the length (not the substring). So I have two strings, say: S = ABAZDC, T = BACBAD Longest common subsequence is ABAD (substrings don't have to be adjacent letters) Algorithm is as follows, where LCS[i, j] denotes longest common subsequence of S[1..i] and T[1..j]: if S[i] != T[j] then LCS[i, j] = max(LCS[i - 1, j], LCS[i,

Can recursion be dynamic programming?

我是研究僧i 提交于 2019-12-24 13:16:33
问题 I was asked to use dynamic programming to solve a problem. I have mixed notes on what constitutes dynamic programming. I believe it requires a "bottom-up" approach, where smallest problems are solved first. One thing I have contradicting information on, is whether something can be dynamic programming if the same subproblems are solved more than once, as is often the case in recursion. For instance. For Fibonacci, I can have a recursive algorithm: RecursiveFibonacci(n) if (n=1 or n=2) return 1

Given a sequence of N numbers ,extract number of sequences of length K having range less than R?

心已入冬 提交于 2019-12-24 13:02:16
问题 There is an array of integers ,I have to find the number of sequences of K length having range ( max - min of the subsequence) less than equal to R .Is there a relation between Number of sequences of length k and number of sequences of length K-1 ? I am trying to solve a practice question on SPOJ. I don't want the full solution,just point me in the right direction /suggestion/hint. I was thinking of a deque like structure to maintain min and max elements of the array upto a certain index

SPOJ - INUMBER (Can't seem to develop a solution within the time limit)

折月煮酒 提交于 2019-12-24 11:36:23
问题 I'm trying to solve this problem on SPOJ INUMBER. Problem statement is as follows: For the given number n find the minimal positive integer divisable by n , with the sum of digits equal to n . INPUT t – the number of test cases, then t test cases follow. (t <= 50) Test case description: n - integer such that 0 < n <= 1000 OUTPUT For each test case output the required number (without leading zeros). EXAMPLE: Input: 2 1 10 Output: 1 190 I can only think of a brute force solution emulating the

Filling a matrix using parallel processing in Julia

限于喜欢 提交于 2019-12-24 10:58:30
问题 I'm trying to speed up the solution time for a dynamic programming problem in Julia (v. 0.5.0), via parallel processing. The problem involves choosing the optimal values for every element of a 1073 x 19 matrix at every iteration, until successive matrix differences fall within a tolerance. I thought that, within each iteration, filling in the values for each element of the matrix could be parallelized. However, I'm seeing a huge performance degradation using SharedArray , and I'm wondering if

Unable to understand algorithm for Longest Increasing Sub-sequence

喜夏-厌秋 提交于 2019-12-24 09:40:11
问题 I have been through many online resources to understand how the problem has the optimal sub-structure, but all in vain, I am unable to comprehend how the solution is obtained by solving smaller sub-problems in this case. I would be thankful if any explanation helps in understanding the solution. so far, I understand the optimal sub-structure property as follows: Example Factorial: So for a factorial of 40 ,fact(40), we can achieve the solution by calculating fact(39)*40, and so on for 39,38..