dynamic-programming

Finding minimal absolute sum of a subarray

梦想的初衷 提交于 2019-12-18 04:42:22
问题 There's an array A containing (positive and negative) integers. Find a (contiguous) subarray whose elements' absolute sum is minimal, e.g.: A = [2, -4, 6, -3, 9] |(−4) + 6 + (−3)| = 1 <- minimal absolute sum I've started by implementing a brute-force algorithm which was O(N^2) or O(N^3) , though it produced correct results. But the task specifies: complexity: - expected worst-case time complexity is O(N*log(N)) - expected worst-case space complexity is O(N) After some searching I thought that

Generate 10-digit number using a phone keypad

我的梦境 提交于 2019-12-17 22:19:24
问题 Given a phone keypad as shown below: 1 2 3 4 5 6 7 8 9 0 How many different 10-digit numbers can be formed starting from 1? The constraint is that the movement from 1 digit to the next is similar to the movement of the Knight in a chess game. For eg. if we are at 1 then the next digit can be either 6 or 8 if we are at 6 then the next digit can be 1, 7 or 0. Repetition of digits are allowed - 1616161616 is a valid number. Is there a polynomial time algorithm which solves this problem? The

Stuck with an interview Question… Partitioning of an Array

半城伤御伤魂 提交于 2019-12-17 22:16:50
问题 I found the following problem on the internet, and would like to know how I would go about solving it: Problem: Integer Partition without Rearrangement Input: An arrangement S of non negative numbers {s 1 , . . . , s n } and an integer k. Output: Partition S into k or fewer ranges, to minimize the maximum of the sums of all k or fewer ranges, without reordering any of the numbers.* Please help, seems like interesting question... I actually spend quite a lot time in it, but failed to see any

how to find the number of distinct subsequences of a string?

元气小坏坏 提交于 2019-12-17 22:04:41
问题 Here is another spoj problem that asks how to find the number of distinct subsequences of a string ? For example, Input AAA ABCDEFG CODECRAFT Output 4 128 496 How can I solve this problem ? 回答1: It's a classic dynamic programming problem. Let: dp[i] = number of distinct subsequences ending with a[i] sum[i] = dp[1] + dp[2] + ... + dp[i]. So sum[n] will be your answer. last[i] = last position of character i in the given string. A null string has one subsequence, so dp[0] = 1 . read a n = strlen

A simple example for someone who wants to understand Dynamic Programming [closed]

可紊 提交于 2019-12-17 21:37:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I am looking for a manageably understandable example for someone who wants to learn Dynamic Programming. There are nice answers here about what is dynamic programming. The fibonacci sequence is a great example, but it is too small to scratch the surface. It looks a great subject to learn about although I haven't

Difference between Divide and Conquer Algo and Dynamic Programming

泄露秘密 提交于 2019-12-17 21:37:34
问题 What is the difference between Divide and Conquer Algorithms and Dynamic Programming Algorithms? How are the two terms different? I do not understand the difference between them. Please take a simple example to explain any difference between the two and on what ground they seem to be similar. 回答1: Divide and Conquer Divide and Conquer works by dividing the problem into sub-problems, conquer each sub-problem recursively and combine these solutions. Dynamic Programming Dynamic Programming is a

How to find the shortest simple path in a Tree in a linear time?

醉酒当歌 提交于 2019-12-17 18:56:35
问题 Here is a problem from Algorithms book by Vazirani The input to this problem is a tree T with integer weights on the edges. The weights may be negative, zero, or positive. Give a linear time algorithm to find the shortest simple path in T. The length of a path is the sum of the weights of the edges in the path. A path is simple if no vertex is repeated. Note that the endpoints of the path are unconstrained. HINT: This is very similar to the problem of finding the largest independent set in a

divide list in two parts that their sum closest to each other

孤人 提交于 2019-12-17 18:41:56
问题 This is a hard algorithms problem that : Divide the list in 2 parts (sum) that their sum closest to (most) each other list length is 1 <= n <= 100 and their(numbers) weights 1<=w<=250 given in the question. For example : 23 65 134 32 95 123 34 1.sum = 256 2.sum = 250 1.list = 1 2 3 7 2.list = 4 5 6 I have an algorithm but it didn't work for all inputs. init. lists list1 = [], list2 = [] Sort elements (given list) [23 32 34 65 95 123 134] pop last one (max one) insert to the list which differs

Number of ways to add up to a sum S with N numbers

拥有回忆 提交于 2019-12-17 18:28:17
问题 Say S = 5 and N = 3 the solutions would look like - <0,0,5> <0,1,4> <0,2,3> <0,3,2> <5,0,0> <2,3,0> <3,2,0> <1,2,2> etc etc. In the general case, N nested loops can be used to solve the problem. Run N nested loop, inside them check if the loop variables add upto S. If we do not know N ahead of time, we can use a recursive solution. In each level, run a loop starting from 0 to N, and then call the function itself again. When we reach a depth of N, see if the numbers obtained add up to S. Any

Getting the submatrix with maximum sum?

混江龙づ霸主 提交于 2019-12-17 04:13:00
问题 Input : A 2-dimensional array NxN - Matrix - with positive and negative elements. Output : A submatrix of any size such that its summation is the maximum among all possible submatrices. Requirement : Algorithm complexity to be of O(N^3) History: With the help of the Algorithmist, Larry and a modification of Kadane's Algorithm, i managed to solve the problem partly which is determining the summation only - below in Java. Thanks to Ernesto who managed to solve the rest of the problem which is