dynamic-programming

A Dynamic programming problem

雨燕双飞 提交于 2019-12-03 18:42:31
问题 Can anyone help me find an optimal Dynamic programming algorithm for this problem On the way to dinner, the CCC competitors are lining up for their delicious curly fries. The N (1 ≤ N ≤ 100) competitors have lined up single-file to enter the cafeteria. Doctor V, who runs the CCC, realized at the last minute that programmers simply hate standing in line next to programmers who use a different language. Thankfully, only two languages are allowed at the CCC: Gnold and Helpfile. Furthermore, the

box stacking problem

喜欢而已 提交于 2019-12-03 17:22:12
Given are n boxes in three dimension ( h , w , d ). The goal is to stack them on top of each other to have a maximum height (boxes can be rotated). Each box that you put on top should have a smaller dimension ( w , d ) than the one below. How can we do it with dynamic programming and greedy? This is the box stacking problem - problem 4 there. If you want to think about it yourself, think about how you can adapt the longest increasing subsequence algorithm for solving this. 来源: https://stackoverflow.com/questions/4511086/box-stacking-problem

Given a number N, find the number of ways to write it as a sum of two or more consecutive integers

99封情书 提交于 2019-12-03 17:00:02
Here is the problem that tagged as dynamic-programming (Given a number N, find the number of ways to write it as a sum of two or more consecutive integers) and example 15 = 7+8, 1+2+3+4+5, 4+5+6 I solved with math like that : a + (a + 1) + (a + 2) + (a + 3) + ... + (a + k) = N (k + 1)*a + (1 + 2 + 3 + ... + k) = N (k + 1) a + k (k+1)/2 = N (k + 1)*(2*a + k)/2 = N Then check that if N divisible by (k+1) and (2*a+k) then I can find answer in O(sqrt(N)) time Here is my question how can you solve this by dynamic-programming ? and what is the complexity (O) ? P.S : excuse me, if it is a duplicate

Longest subsequence of S that is balanced

那年仲夏 提交于 2019-12-03 15:45:34
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: Suppose given string: S[1 2 ... n] A valid sub-sequence can end at S[i] iff S[i] == ')' i.e. S[i] is a

Sub-sequence of Vowels

牧云@^-^@ 提交于 2019-12-03 14:08:50
I was practicing for an interview and came across this question on a website: A magical sub-sequence of a string S is a sub-sequence of S that contains all five vowels in order. Find the length of largest magical sub-sequence of a string S . For example, if S = aeeiooua , then aeiou and aeeioou are magical sub-sequences but aeio and aeeioua are not. I am a beginner in dynamic programming and am finding it hard to come up with a recursive formula for this. I did it with an iterative approach rather than recursive one. I started building solution similar to LIS (Longest Increasing Subsequence)

Is “house coloring with three colors” NP?

匆匆过客 提交于 2019-12-03 13:34:23
问题 Consider the problem described here (reproduced below.) Can some better known NP-complete problem be reduced to it? The problem: There are a row of houses. Each house can be painted with three colors: red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with minimum cost. How would you do it? Note: The cost of painting house 1 red is different

Parallel Dynamic Programming

那年仲夏 提交于 2019-12-03 13:25:19
问题 Are there any good papers discussing how to take a dynamic program and parallelize it? 回答1: IIRC, what you typically do with dynamic programming is to recursively divide a problem into subproblems, and assemble optimal solutions from optimal subsolutions. What makes it effective is that all optimal subsolutions are built into a cache so they need not be recomputed. If the problem can be divided several ways, you can fork the solver for each subsolution. If each(sub) problem averages 1+epsilon

Given a set of ranges S, and an overlapping range R, find the smallest subset in S that encompases R

爱⌒轻易说出口 提交于 2019-12-03 13:00:50
The following is a practice interview question that was given to me by someone, and I'm not sure what the best solution to this is: Given a set of ranges: (e.g. S = {(1, 4), (30, 40), (20, 91) ,(8, 10), (6, 7), (3, 9), (9, 12), (11, 14)} . And given a target range R (e.g. R = (3, 13) - meaning the range going from 3 to 13). Write an algorithm to find the smallest set of ranges that covers your target range. All of the ranges in the set must overlap in order to be considered as spanning the entire target range. (In this example, the answer would be {(3, 9), (9, 12), (11, 14)} . What is the best

Dynamic programming exercise for string cutting

我只是一个虾纸丫 提交于 2019-12-03 12:34:33
问题 I have been working on the following problem from this book. A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20

Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)?

妖精的绣舞 提交于 2019-12-03 12:12:53
The question is to find out sum of distances between every two nodes of BinarySearchTree given that every parent-child pair is separated by unit distance. It is to be calculated after every insertion. ex: ->first node is inserted.. (root) total sum=0; ->left and right node are inserted (root) / \ (left) (right) total sum = distance(root,left)+distance(root,right)+distance(left,right); = 1 + 1 + 2 = 4 and so on..... Solutions I came up with: Brute-force. Steps: perform a DFS and track all the nodes : O(n) . Select every two nodes and calculate : O(nC2)_times_O(log(n))=O(n2log(n)) distance