dynamic-programming

Knapsack C# implementation task

▼魔方 西西 提交于 2019-12-11 00:57:18
问题 I'm trying to write knapsack c# algorithm with given conditions but there's always two problems i encountering. I'm getting "Index was outside the bounds of the array" error or my result is just 0. I found couple code examples of Knapsack implementation and just can't figure out what I'm doing wrong. Code examples: https://www.programmingalgorithms.com/algorithm/knapsack-problem http://www.csharpstar.com/csharp-knapsack-problem/ My code: static int Knapsack(int n, int w, int[] s, int[] v) {

Maximizing arithmetic expression

半世苍凉 提交于 2019-12-10 23:10:44
问题 I want to maximize the expression 5-8+7*4-8+9 and answer is 200 after splitting this way (5 − ((8 + 7) × (4 − (8 + 9)))) . It can be solved by using Matrix-chain multiplication algorithm. It gives correct answer if expression has only '+' and '*' operator Let's take expression 5+2*4 1 2 3 1 5 7 28 2 - 2 8 3 - - 4 It's a 3X3 Matrix in which (1,1) is 5 ,(2,2) is 2 and (3,3) is 4 and if i want to know M[1][2] or M[1][3] then M[1][2] = M[1][1] o M[2][2] M[1][3] = max(M[1][1] o M[2][3],M[1][2] o M

Recursive Relation Help for dynamic programming 2d Plane algorithm

感情迁移 提交于 2019-12-10 17:54:30
问题 So I've been working on an algorithm. The task I'm trying to accomplish is: consider a 2D plane there are targets that are randomly distributed between a y upper bound and lower bound. This set is T. T1 is marked with coordinates(X,Y). There is a set S of sensors that are guaranteed to cover every target each sensor has a radius 1 and an (X,Y) coordinate. Each target has a cost (c) that is a weight. So my task is to find a minimum weight or cost for a set S' that covers each sensor. So I know

Find sum of largest subset of array

戏子无情 提交于 2019-12-10 17:45:44
问题 I'm solving one CS problem, namely we have given array with size N, such that N<=100000, and the array will have both negative and positive integers, now we have to find the sum of the largest subset of the array, or more formally we have to find the indexes i and j such that the sum of the elements between those elements will me maximum possible. Here is one example: N=5, array={12, -4, -10, 4, 9}, answer = 13, because 4+9 is the best we can get. I know that this can be solved by bruteforce

Coin change(Dynamic programming)

醉酒当歌 提交于 2019-12-10 17:36:13
问题 I have a question about the coin change problem where we not only have to print the number of ways to change $n with the given coin denominations for eg {1,5,10,25}, but also print the ways For example if the target = $50, and the coins are {1,5,10,25} , then the ways to actually get use the coins to get the target are 2 × $25 1 × $25 + 2 × $10 + 1 × $5 etc. What is the best time complexity we could get to solve this problem? I tried to modify the dynamic programming solution for the coin

Solving Dynamic Programming Problem on coins

二次信任 提交于 2019-12-10 16:59:34
问题 Consider a below problem Given an infinite number of nickels (5 cents) and pennies (1 cent). Write a code to calculate a number of ways of representing n cents. My code def coins(n): if (n < 0): return 0 elif (n == 0): return 1 else: if (cnt_lst[n-1] == 0): cnt_lst[n-1] = coins(n-1) + coins(n-5) return cnt_lst[n-1] if __name__ == "__main__": cnt = int(input()) cnt_lst = [0] * cnt #Memiozation ret = coins(cnt) print(ret) Above approach counting repeated patterns more than one (obviously I'm

Calculating the complexity of Levenshtein Edit Distance

我的未来我决定 提交于 2019-12-10 16:48:17
问题 I have been looking at this simple python implementation of Levenshtein Edit Distance for all day now. def lev(a, b): """Recursively calculate the Levenshtein edit distance between two strings, a and b. Returns the edit distance. """ if("" == a): return len(b) # returns if a is an empty string if("" == b): return len(a) # returns if b is an empty string return min(lev(a[:-1], b[:-1])+(a[-1] != b[-1]), lev(a[:-1], b)+1, lev(a, b[:-1])+1) From: http://www.clear.rice.edu/comp130/12spring

2 knapsacks with same capacity - Why can't we just find the max-value twice

我的未来我决定 提交于 2019-12-10 16:39:06
问题 If you are given one set of items with values and weight: [(w1,v2),(w2,v2),...(wn,vn)], and two knapsacks Knap1 and Knap2 of equal capacity C, the goal is to determine what are the optimal subsets of items S1 and S2 that can go into Knap1 and Knap2 respectively and maximize the knapsacks' values and capacity. An incorrect way to solve this would be to first fill Knap1 with a DP programming algorithm using all of the items as candidates, and then fill Knap2 by using the leftover items from

All possible LCS(Longest Common Subsequence) of two strings

我的梦境 提交于 2019-12-10 16:05:33
问题 We can find the LCS(Longest Common Subsequence) of two strings with DP(Dynamic Programming). By keeping track with DP Table we can get the LCS. But if there exists more than one LCS how can we get all of them? Example: string1 : bcab string2 : abc Here both "ab" and "bc" are LCS. 回答1: Here is a working java solution. For explanation you can see my answer How to print all possible solutions for Longest Common subsequence static int arr[][]; static void lcs(String s1, String s2) { for (int i =

Longest Increasing Subsequence, Dynamic Programing

橙三吉。 提交于 2019-12-10 12:21:18
问题 I have the following problem: Find the longest increasing subsequence of a given sequence / array. In other words, find a subsequence of array in which the subsequence’s elements are in strictly increasing order, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique. In this case, we only care about the length of the longest increasing subsequence. Example : Input : [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] Output : 6 The