dynamic-programming

Dynamic Programming Coin Change Limited Coins

随声附和 提交于 2019-12-23 12:39:21
问题 Dynamic Programming Change Problem (Limited Coins) . I'm trying to create a program that takes as INPUT: int coinValues[]; //e.g [coin1,coin2,coin3] int coinLimit[]; //e.g [2 coin1 available,1 coin2 available,...] int amount; //the amount we want change for. OUTPUT: int DynProg[]; //of size amount+1. And output should be an Array of size amount+1 of which each cell represents the optimal number of coins we need to give change for the amount of the cell's index. EXAMPLE: Let's say that we have

What kind of algorithm is this? Box packing/Knapsack?

那年仲夏 提交于 2019-12-23 12:29:38
问题 I was working on an application last night and came across a particular problem which I'm sure probably has an efficient algorithm to solve it. Could anyone suggest? Problem: TL;DR: Maybe a picture will help: http://www.custom-foam-inserts.com/ . I have lots of items which fit in a range of compartments: and I want to minimise the number of cases I need to take. . I have a set of N items of expensive electronic equipment which I want to pack into specially designed protective boxes. These

Finding shortest combinations in array/sequence that equals sum

拜拜、爱过 提交于 2019-12-23 09:30:50
问题 I'm totally stuck and have no idea how to go about solving this. Let's say I've an array arr = [1, 4, 5, 10] and a number n = 8 I need shortest sequence from within arr which equals n. So for example following sequences within arr equals n c1 = 5,1,1,1 c2 = 4,4 c3= 1,1,1,1,1,1,1,1 So in above case, our answer is c2 because it's shortest sequences in arr that equals sum. I'm not sure what's the simplest way of finding a solution to above? Any ideas, or help will be really appreciated. Thanks!

Coin change DP solution to keep track of coins

眉间皱痕 提交于 2019-12-23 09:27:19
问题 Trying to program a DP solution for the general coin-change problem that also keeps track of which coins are used. So far I have it working to give me the minimum amount of coins needed but can't figure out how to get which coins were used and how many times. I tried setting up another table (boolean) with values if the coin is used but that doesn't seem to work correctly. Any ideas? public static int minChange(int[] denom, int changeAmount) { int m = denom.length; int n = changeAmount + 1;

Number of students with better grades and lower jee rank

笑着哭i 提交于 2019-12-23 04:17:13
问题 We are given n students with cgpa (college grades) and jee ranks (Rank in admission exam) of every student. For every student, we have to calculate the number of students who have better cgpa but worse jee rank. (x1,y1), (x2,y2) ...(xi,yi)... (xn,yn) for each i, we have to calculate no. of j for which xj > xi and yj > yi (worse rank means greater rank.) I could come up with the following nlogn algorithm- Sort them decreasing cgpa. Now start scanning from left. Maintain the students scanned so

Dynamic Programming - Primitive Calculator Python [duplicate]

隐身守侯 提交于 2019-12-23 02:39:12
问题 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

Dynamic Programming - Primitive Calculator Python [duplicate]

泄露秘密 提交于 2019-12-23 02:39:04
问题 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

Directed graph linear algorithm

有些话、适合烂在心里 提交于 2019-12-23 02:30:49
问题 I would like to know the best way to calculate the length of the shortest path between vertex s and every other vertex of the graph in linear time using dynamic programming. The graph is weighted DAG. 回答1: What you can hope for is an algorithm linear in the number of edges and vertices, i.e. O(|E| + |V|) , which also works correctly in presence of negative weights. This is done by first computing a topological order and then 'exploring' the graph in the order given by this topological order.

How to find a subarray with minimum k length and maximum sum?

不羁的心 提交于 2019-12-22 09:49:42
问题 The subarray contains both positive and negative numbers. You have to find a maximum sum subarray such that the length of the sub-array is greater than or equal to k. Here is my code in c++ using Kadane's algorithm. #include <iostream> using namespace std; int main(){ int n,k; cin >> n >> k; int array[n]; int sum = 0; int maxsum = 0; int beststarts[n]; for(int i = 0;i < n; i++){ cin >> array[i]; } for(int i = 0;i < k-1;i ++){ sum = sum+array[i]; beststarts[i] = 0; } for(int i = k-1;i < n; i++

Faster edit distance algorithm [closed]

旧街凉风 提交于 2019-12-22 08:33:27
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Problem: I know the trivial edit distance DP formulation and computation in O(mn) for 2 strings of size n and m respectively. But I recently came to know that if we only need to calculate the minimum value of edit distance f and it is bounded |f|<=s, then we can calculate it in O