dynamic-programming

Longest subsequence that first increases then decreases

别等时光非礼了梦想. 提交于 2019-12-22 07:02:52
问题 I am trying to solve the following question : A sequence in which the value of elements first decrease and then increase is called V- Sequence. In a valid V-Sequence there should be at least one element in the decreasing and at least one element in the increasing arm. For example, "5 3 1 9 17 23" is a valid V-Sequence having two elements in the decreasing arm namely 5 and 3, and 3 elements in the increasing arm namely 9, 17 and 23 . But none of the sequence "6 4 2" or "8 10 15" are V-Sequence

Improve C++ Fibonacci series

这一生的挚爱 提交于 2019-12-22 00:17:45
问题 I know that: int fib(int n) { if (n == 0 || n == 1) return 1; return fib(n − 1)+ fib(n − 2); } when n=5,fib(5) evaluates as: fib(5) fib(4) + fib(3) (fib(3) + fib(2)) + (fib(2) + fib(1)) ((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) (((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) Notice that each base element being used for several times, is there a way to use map to store the previous value and simply do fib(n − 1) + fib(n − 2)? 回答1:

Special Pairs with sum as Prime Number

怎甘沉沦 提交于 2019-12-22 00:15:54
问题 A number N is given in the range 1 <= N <= 10^50 . A function F(x) is defined as the sum of all digits of a number x. We have to find the count of number of special pairs (x, y) such that: 1. 0 <= x, y <= N 2. F(x) + F(y) is prime in nature We have to count (x, y) and (y, x) only once. Print the output modulo 1000000000 + 7 My approach: Since the maximum value of sum of digits in given range can be 450 (If all the characters are 9 in a number of length 50, which gives 9*50 = 450 ). So, we can

Minimum repetitions in merged array of characters

跟風遠走 提交于 2019-12-21 23:28:40
问题 Suppose I have two arrays and I want to merge them so that the merged array has the minimum amount of repetitions . For example [ 'x', 'x' ] is a repetition. arr1 = [ 'x', 'd', 'd', 'm', 'f', 'm' ] arr2 = [ 'd', 'd', 'x', 'f', 'f', 'm' ] The only condition is that in the merged array, the elements from arr1 and arr2 must appear in their respective orders within arr1 and arr2 . Below is an example of the merged array with 0 repetitions while maintaining this condition. merged = [ 'd', 'x', 'd'

Unable to implement a dynamic programming table algorithm in python

只愿长相守 提交于 2019-12-21 22:53:23
问题 I am having problems creating a table in python. Basically I want to build a table that for every number tells me if I can use it to break down another(its the table algo from the accepted answer in Can brute force algorithms scale?). Here's the pseudo code: for i = 1 to k for z = 0 to sum: for c = 1 to z / x_i: if T[z - c * x_i][i - 1] is true: set T[z][i] to true Here's the python implementation I have: from collections import defaultdict data = [1, 2, 4] target_sum = 10 # T[x, i] is True

Trying to figure out the classic Knapsack recurrence

こ雲淡風輕ζ 提交于 2019-12-21 21:53:57
问题 I am reading about the Knapsack Problem (unbounded) which is, as I understand a classic in DP. Although I think I understand the solution as I read it, I am not clear how I can translate it to actual code. For example in the following recurrence "formula": M(j) = MAX {M(j-1), MAX i = 1 to n (M(j - Si) + Vi) } for j >=1 I am not sure how I can translate this to code, since it is not clear to me if the inner MAX should be there or it should just be instead: M(j) = MAX {M(j-1), M(j - Si) + Vi }

Maximum sum of non adjacent elements of an array, to be printed

让人想犯罪 __ 提交于 2019-12-21 20:49:26
问题 There is an array of integers {1,2,3,-1,-3,2,5}, my job is to print the elements which leads to max sum of the sub array, the sum obtained is by adding non adjacent elements in the array. I written the code to give the max sum, using dynamic programming. but not able to print the elements. public static int maxSum(int arr[]) { int excl = 0; int incl = arr[0]; for (int i = 1; i < arr.length; i++) { int temp = incl; incl = Math.max(Math.max(excl + arr[i], arr[i]), incl); excl = temp; } return

Improving random memory access when random access is needed

南楼画角 提交于 2019-12-21 20:44:27
问题 The basic concept of what I am doing Complete coalition structure formation problem/Combinatorial auctions. Given a set of N agents, which disjoint subsets of the set of agents yields the best outcome. E.g. Agents = {a,b} and their values {a} = 2 {b} = 3 {a,b} = 4 In this instance the coalition of {{a},{b}} = 5 would give the best outcome, where it is the pairwise disjoint subset of {a,b} . So in short the problem is about splitting a set and check if any of the splittings sum is greater than

partition of a list using dynamic programming

此生再无相见时 提交于 2019-12-21 18:57:34
问题 I have posted a bit here related to a project I have been trying to work on and I keep hitting design problems and have to design from scratch. So I'm wondering if I can post what I'm trying to do and someone can help me understand how I can get the result I want. BackGround: I'm new to programming and trying to learn. So I took a project that interested me which involves basically taking list and breaking down each number using only numbers from the list. I know I could easily brute force

How to implement a dynamic programming algorithms to TSP in Python?

穿精又带淫゛_ 提交于 2019-12-21 18:04:07
问题 I want to solve the TSP problem using a dynamic programming algorithm in Python.The problem is: Input: cities represented as a list of points. For example, [(1,2), (0.3, 4.5), (9, 3)...]. The distance between cities is defined as the Euclidean distance. Output: the minimum cost of a traveling salesman tour for this instance, rounded down to the nearest integer. And the pseudo-code is: Let A = 2-D array, indexed by subsets of {1, 2, ,3, ..., n} that contains 1 and destinations j belongs to {1,