dynamic-programming

How to solve this question asked in premium coding contest?

不打扰是莪最后的温柔 提交于 2020-08-19 05:49:48
问题 Could someone guide me on how to solve this programming question? It seems something like DP problem which I couldn't find answer for. Question: There are ‘n’ ads. Each ad has an effectiveness value associated with it which is given in an array of size ‘n’ in the format [v1, v2, …, vn], where ‘v1’ is the effectiveness value of the first ad, ‘v2’ is the effectiveness value of the second ad, and so on. The show in which these ads will be shown is ‘m’ length long (starting from 0 till m), and

Dynamic Programming: Why Knuth's improvement to Optimal Binary Search Tree O(n^2)?

主宰稳场 提交于 2020-07-17 06:30:53
问题 This is Exercise 15.5-4 of Introduction to Algorithms, 3rd edition, which is about Knuth's improvement to the DP approach to Optimal Binary Search Tree. The DP algorithm of Optimal Binary Search Tree is: OPTIMAL_BST(p, q, n) let e[1..n+1, 0..n], w[1..n+1, 0..n], and root[1..n, 1..n] be new tables for i = 1 to n+1 e[i, i - 1] = q[i - 1]; w[i, i - 1] = q[i - 1]; for l = 1 to n for i = 1 to n - l + 1 j = i + l - 1 e[i, j] = INFINITY w[i, j] = w[i, j - 1] + p[j] + q[j] for r = i to j t = e[i, r -

Dynamic Programming: Why Knuth's improvement to Optimal Binary Search Tree O(n^2)?

强颜欢笑 提交于 2020-07-17 06:30:47
问题 This is Exercise 15.5-4 of Introduction to Algorithms, 3rd edition, which is about Knuth's improvement to the DP approach to Optimal Binary Search Tree. The DP algorithm of Optimal Binary Search Tree is: OPTIMAL_BST(p, q, n) let e[1..n+1, 0..n], w[1..n+1, 0..n], and root[1..n, 1..n] be new tables for i = 1 to n+1 e[i, i - 1] = q[i - 1]; w[i, i - 1] = q[i - 1]; for l = 1 to n for i = 1 to n - l + 1 j = i + l - 1 e[i, j] = INFINITY w[i, j] = w[i, j - 1] + p[j] + q[j] for r = i to j t = e[i, r -

Find number of ways to create sequence A of length n satisfying m conditions

故事扮演 提交于 2020-07-16 05:20:24
问题 Find number of ways to create sequence A of length n satisfying m conditions. This sequence A should consist of only non negative numbers. Each condition is described by three integers (i,j,k) signifying max(A[i],A[j])=k. It is guaranteed that each index of the sequence will be there in at least one condition i.e. there will be finite number of such sequences. The maximum value of n will not exceed 18 and maximum value of k will not exceed 20000. I tried it using dynamic programming but the

Sliding window maximum in O(n) time

落爺英雄遲暮 提交于 2020-07-14 07:06:50
问题 Input: listi = [9, 7, 8, 4, 6, 1, 3, 2, 5] Output: # m=3 listo = [9, 8, 8, 6, 6, 3, 5] Given a random list composed of n numbers, I need to find all of the sublists of m consequtive elements, choose the largest value from the sublist and put them in a new list. def convert(listi, m): listo = [] n = len(listi) for i in range(n-m+1): listo.append(max(listi[i:3+i])) return listo The time complexity for this implementation is O(m\^{(n-m+1)} , which is pretty bad if listi is long, is there a way

Number of Common sub sequences of two strings

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-07 12:05:30
问题 Revisiting Longest Common Subsequence, I was wondering what is the number of common subsequences of 2 strings. I tried to formulate a recurrent relation : DP[i][j] represents the number of subsequences ending at max i in first string and j in second. Recurrent relation being : DP[i][j]= DP[i-1][j-1] + DP[i][j-1] + DP[i-1][j] when A[i]==B[j] (A is my first string while B is the second) and DP[i][j]=DP[i-1][j]+DP[i][j-1] other wise It is not giving correct results! Explanation if A[i]==B[j],

Number of Common sub sequences of two strings

喜夏-厌秋 提交于 2020-07-07 12:05:28
问题 Revisiting Longest Common Subsequence, I was wondering what is the number of common subsequences of 2 strings. I tried to formulate a recurrent relation : DP[i][j] represents the number of subsequences ending at max i in first string and j in second. Recurrent relation being : DP[i][j]= DP[i-1][j-1] + DP[i][j-1] + DP[i-1][j] when A[i]==B[j] (A is my first string while B is the second) and DP[i][j]=DP[i-1][j]+DP[i][j-1] other wise It is not giving correct results! Explanation if A[i]==B[j],

Optimization problem - How to add same team constraint

北城余情 提交于 2020-06-29 04:34:39
问题 How the dataset looks like: I'm trying to build an optimization tool for fantasy football, but I'm having difficulty forcing the model to use players from the same team. 9 players form a lineup, need to be under 50k and we are maximizing "proj" projected points self.salary_cap = 50000 self.Minsalary_cap = 0 self.header = ['QB', 'RB', 'RB','WR', 'WR', 'WR', 'TE','FLEX', 'Def'] #define the pulp object problem prob = pulp.LpProblem('NFL', pulp.LpMaximize) #define the player variabless players

Multidimensional/multivariate dynamic time warping (DTW) library/code in Python

那年仲夏 提交于 2020-06-09 18:05:54
问题 I am working on a time series data. The data available is multi-variate. So for every instance of time there are three data points available. Format: | X | Y | Z | So one time series data in above format would be generated real time. I am trying to find a good match of this real time generated time series within another time series base data, which is already stored (which is much larger in size and was collected at a different frequency). If I apply standard DTW to each of the series (X,Y,Z)

Can Bellman Ford Algorithm have any arbitary order of edges?

匆匆过客 提交于 2020-05-27 05:39:07
问题 I have just started learning new algorithms but I got stuck when I read bellman ford algorithms on geeks for geeks :- http://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/ There It is written that- the algorithm calculate shortest paths in bottom-up manner. It first calculates the shortest distances for the shortest paths which have at-most one edge in the path. Then, it calculates shortest paths with at-most 2 edges, and so on. After the ith iteration of outer loop,