dynamic-programming

Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)

纵饮孤独 提交于 2020-01-01 06:17:12
问题 Practically, I've got a set of objects with probabilities, and I want to look at each possible group of them, in order of how likely it is that they're all true assuming they're independent -- i.e. in descending order of the product of the elements of the subsets -- or in order of length if the probabilities are the same (so that (1, 0.5) comes after (0.5)). Example: If I have [ 1, 0.5, 0.1 ] I want [ (), (1), (0.5), (1, 0.5), (0.1), (1, 0.1), (0.5, 0.1), (1, 0.5, 0.1) ] In essence, this

Longest subsequence of S that is balanced

空扰寡人 提交于 2020-01-01 05:31:05
问题 Given question: A string of parentheses is said to be balanced if the left- and right-parentheses in the string can be paired off properly. For example, the strings "(())" and "()()" are both balanced, while the string "(()(" is not balanced. Given a string S of length n consisting of parentheses, suppose you want to find the longest subsequence of S that is balanced. Using dynamic programming, design an algorithm that finds the longest balanced subsequence of S in O(n^3) time. My approach:

How to draw dynamic programming table in python

一笑奈何 提交于 2019-12-31 17:23:41
问题 What is a good way to draw a dynamic programming such as this one (with the path) in python? I have looked online and I see pygame but is that really the best option for this sort of technical drawing? One option might be to use matplotlib using something like import matplotlib.pylab as plt plt.figure() col_labels=['col1','col2','col3'] row_labels=['row1','row2','row3'] table_vals=[[11,12,13],[21,22,23],[31,32,33]] the_table = plt.table(cellText=table_vals, colWidths = [0.1]*3, rowLabels=row

Finding the minimum length RLE

删除回忆录丶 提交于 2019-12-31 16:49:44
问题 The classical RLE algorithm compresses data by using numbers to represent how many times the character following a number appears in the text at that position. For example: AAABBAAABBCECE => 3A2B3A2B1C1E1C1E However, in the above example, that method results in even more space being used by the compressed text. A better idea would be to use numbers to represent how many times the substring following a number appears in the given text. For example: AAABBAAABBCECE => 2AAABB2CE ("AAABB" twice,

Finding the Longest Palindrome Subsequence with less memory

倖福魔咒の 提交于 2019-12-31 09:04:56
问题 I am trying to solve a dynamic programming problem from Cormem's Introduction to Algorithms 3rd edition (pg 405) which asks the following: A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic , racecar , and aibohphobia (fear of palindromes). Give an efficient algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input character , your

Upper limits for fibonnacci

孤街醉人 提交于 2019-12-31 02:31:51
问题 I was reading about the DP version of fibonnaci. In Sedgewick I saw: int[] T = new int[47]; for storage of the previous calculations. Elsewhere I saw that the max input for fibonacci should be less than 92 . It is not clear to me how does these numbers come up? I understand that it has to do with overflow and size of int but I am not clear how we end up with these limits. Any help? 回答1: Well, the fibonacci series grows (approximately) exponentially with a ratio of 1.618 (the golden ratio). If

Find the total number of distinct Non decreasing arrays possible

喜欢而已 提交于 2019-12-30 12:14:14
问题 Given the exact no. of elements that must be present in the array (let=r) and the max value of the last element of the array (let=n) find the total number of distinct non decreasing arrays possible (all elements of array must be >=0) Example- If r=3 and n=2 then some possible non decreasing arrays are {0,0,2},{0,0,1},{0,0,0},{1,2,2} etc. I need the no. of these kind of arrays possible. I tried to solve it using recursion and memoization but it is too slow. here is my code ( ll means long long

longest palindromic substring recursive solution

ぐ巨炮叔叔 提交于 2019-12-30 08:00:09
问题 I am aware of solutions that uses the bottom up dynamic programing approach to solve this problem in O(n^2). I am specifically looking for a top down dp approach. Is it possible to achieve longest palindromic substring using a recursive solution? Here is what I have tried but it fails for certain cases, but I feel I am almost on the right track. #include <iostream> #include <string> using namespace std; string S; int dp[55][55]; int solve(int x,int y,int val) { if(x>y)return val; int &ret =

Partition an array into K subarrays with minimal difference

末鹿安然 提交于 2019-12-30 07:05:31
问题 DISCLAIMER: Described problem looks like a task from a competition. I'm not participating in any of them, I'm not aware about any ongoing competitions, which might involve the problem. If there are any of them, I'll close the question to stay fair! I have a problem: given an array A of values and integer K, split A into exactly K non-overlapping contiguous subarrays in such way that difference between a subarray with minimal and a subarray maximum sums is minimal. It is allowed to rotate A by

Google codejam APAC Test practice round: Parentheses Order

余生长醉 提交于 2019-12-30 06:37:13
问题 I spent one day solving this problem and couldn't find a solution to pass the large dataset. Problem An n parentheses sequence consists of n "("s and n ")"s. Now, we have all valid n parentheses sequences. Find the k-th smallest sequence in lexicographical order. For example, here are all valid 3 parentheses sequences in lexicographical order: ((())) (()()) (())() ()(()) ()()() Given n and k, write an algorithm to give the k-th smallest sequence in lexicographical order. For large data set: 1