dynamic-programming

Minimum no of changes required to make array strictly increasing

爱⌒轻易说出口 提交于 2019-11-29 15:03:49
问题 I have a problem in which we have an array of positive numbers and we have to make it strictly increasing by making zero or more changes to the array elements. We are asked the minimum number of changes required to make the array strictly increasing. Example if array is 1 2 9 10 3 15 so ans=1 if change 3 to some number between 12 to 14. if 1 2 2 2 3 4 5 ans=5 since changing 2 to 3 then 2 to 4 then 3 to 5 then 4 to 6 then 5 to 7 Constraints: Number of elements in array <= 10^6 Each element <=

Shortest two disjoint paths between two specified vertices

女生的网名这么多〃 提交于 2019-11-29 15:00:50
问题 Given a weighted undirected graph G and two vertices a, b , we want to find two paths a -> b and b -> a such that they don't share any edge, and such that the sum of weights of edges in both paths is minimum. There can be up to 1,000 vertices, and up to 10,000 edges. I had initially tried to come up with a dynamic programming approach, but couldn't find such. Any ideas/suggestions would be extremely appreciated. 回答1: This is Minimum-cost flow problem. You can assign flow capacity for each

In python, how does one efficiently find the largest consecutive set of numbers in a list that are not necessarily adjacent?

不想你离开。 提交于 2019-11-29 12:36:54
问题 For instance, if I have a list [1,4,2,3,5,4,5,6,7,8,1,3,4,5,9,10,11] This algorithm should return [1,2,3,4,5,6,7,8,9,10,11]. To clarify, the longest list should run forwards. I was wondering what is an algorithmically efficient way to do this (preferably not O(n^2))? Also, I'm open to a solution not in python since the algorithm is what matters. Thank you. 回答1: Here is a simple one-pass O(n) solution: s = [1,4,2,3,5,4,5,6,7,8,1,3,4,5,9,10,11,42] maxrun = -1 rl = {} for x in s: run = rl[x] =

Given an array of length n, find number of subsets where XOR of a subset is equal to a given number [closed]

不打扰是莪最后的温柔 提交于 2019-11-29 12:19:05
Given an array, arr , of length n , find how many subsets of arr there are such that XOR(^) of those subsets is equal to a given number, ans . I have this dp approach but is there a way to improve its time complexity. ans is always less than 1024. Here ans is the no. such that XOR(^) of the subsets is equal to it. arr[n] contains all the numbers memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for(i = 1; i <= n; i++){ for(j = 0; j < 1024; j++) { dp[i][j] = (dp[i-1][j] + dp[i-1][j^arr[i]]); } } cout << (dp[n][ans]); Mohit Jain From user3386109 's comment, building on top of your code: /* Warning:

Edit distance recursive algorithm — Skiena

偶尔善良 提交于 2019-11-29 12:17:02
问题 I'm reading The Algorithm Design Manual by Steven Skiena, and I'm on the dynamic programming chapter. He has some example code for edit distance and uses some functions which are explained neither in the book nor on the internet. So I'm wondering a) how does this algorithm work? b) what do the functions indel and match do? #define MATCH 0 /* enumerated type symbol for match */ #define INSERT 1 /* enumerated type symbol for insert */ #define DELETE 2 /* enumerated type symbol for delete */ int

Dynamic Programming - Number of distinct combinations to reach a given score

这一生的挚爱 提交于 2019-11-29 12:02:05
Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of 'distinct' combinations to reach the given score. My code: #include <iostream> #include<unordered_map> using namespace std; unordered_map<int,int> m; int numOfWays(int n){ if(n==0) return 1; if(n<0) return 0; if(m[n]>0) return m[n]; m[n] = numOfWays(n-3)+numOfWays(n-5)+numOfWays(n-10); return m[n]; } int main(){ int t; cin>>t; cout<<numOfWays(t)<<endl; return 0; } For input 11, I am getting 3 as output but distinct combinations possible is only 1. (11 = 3 + 3 + 5) How do I modify the

Algorithm to find maximum coverage of non-overlapping sequences. (I.e., the Weighted Interval Scheduling Prob.)

天大地大妈咪最大 提交于 2019-11-29 11:43:24
I have a question that is very similar to algorithm to find longest non-overlapping sequences . The only difference to the linked question is that instead of finding the set of non-overlapping tuples that represent the longest sequence , I need to find the set of non-overlapping tuples that represent the maximum coverage , by which I mean the sum of the tuple lengths is maximum (a tuple length being last - first + 1 given the definition of tuple in the next sentence). I represent my tuples differently than the linked problem. Instead of (starting index, length) , I represent my tuples as

Dynamic Programming in Mathematica: how to automatically localize and / or clear memoized function's definitions

醉酒当歌 提交于 2019-11-29 09:37:01
问题 In Mathematica 8.0, suppose I have some constants: a:=7 b:=9 c:=13 d:=.002 e:=2 f:=1 and I want to use them to evaluate some interlinked functions g[0,k_]:=0 g[t_,0]:=e g[t_,k_]:=g[t-1,k]*a+h[t-1,k-1]*b h[0,k_]:=0 h[t_,0]:=f h[t_,k_]:=h[t-1,k]*c+g[t-1,k-1]*d But this is really slow and in need of dynamic programming, or else you get an exponential slowdown: g[0, k_] := 0 g[t_, 0] := e g[t_, k_] := g[t, k] = g[t - 1, k]*a + h[t - 1, k - 1]*b h[0, k_] := 0 h[t_, 0] := f h[t_, k_] := h[t, k] = h

Find maximum product of 3 numbers in an array

泪湿孤枕 提交于 2019-11-29 09:35:42
问题 Given an array of integers, which can contain both +ve and -ve numbers. I've to maximize the product of any 3 elements of the array. The elements can be non-contiguous. Some examples: int[] arr = {-5, -7, 4, 2, 1, 9}; // Max Product of 3 numbers = -5 * -7 * 9 int[] arr2 = {4, 5, -19, 3}; // Max Product of 3 numbers = 4 * 5 * 3 I've tried solving it using Dynamic Programming , but I'm not getting the expected result. It is returning the result often involving the same number twice in the

Divide array into k contiguos partitions such that sum of maximum partition is minimum

☆樱花仙子☆ 提交于 2019-11-29 08:42:51
Here maximum sum subset is one of k subsets that give maximum sum e.g: arr = [10,5,3,7] and k = 2 possible ways to divide arr in k subsets is {10,[5,3,7]},{[10,5],[3,7},{[10,5,3],7} and {[10,5],[3,7} is the optimal one. Edit: it is equivalent of https://www.codechef.com/DI15R080/problems/MINMAXTF Assume you know the answer is x which means sum of the maximum subset is equal to x . You can verify this assumption by a greedy algorithm O(n) . (Traverse the array from left to right and pick items until the sum of that subset is lower than x ). Now you can binary search on x and find the minimum