Given an array A[1..n] representing a sequence of n integers, a subsequence is a subset of elements of A, in the same order as they appear in A. A subsequence is monotonic if it is a sequence of strictly increasing numbers. Define LMS(i) to be the length of a longest monotonically increasing subsequence of A[1..i] that must have A[i] as its last element. Write a recurrence for LMS(i) and convert into a dynamic program that calculates LMS(i) for i=1..n.
This should be in pseudocode.
Let us first discuss, whehter this problem is of dynamic
programming or not. And to verify that we need to have optimal
substructure
and overlapping subproblems in our quesiton. In given problem one
can easily notice the optimal substructure.
Let us say length[0...n-1] be the array and length[i] indicates the
length of LIS ending at that index. Then you may have noticed
that
we can easily calculate this array in recursive fashion as
indicated below -
length[i]=1+max(length[j]) {where
0<j<i and A[j]<A[i], A[0...n-1] is the input array}
length[i]=1
{when no such j exist}
So solving it recusrsively is quite easy task.
And while solving this sharp eyed reader has easily pointed out the
overlapping subproblems. For every i we will be using the answers
of every
j smaller then that i. So what we will do is nothing but just store
the anser for every j in the array and then we can use it
later
whenever it is required. Now, let us see the pseducode below.
A[0...n-1] is the input array
length[0...n-1] is the dp array that stores the length of LIS
ending at each index.
//declare the length array and initialize the first index as
1
length[0]=1
//calculating the length array
for i = 1 to n
//initialize each length[i] as 1.
length[i]=1
for j = 0 to i
//checking whether current A[j] is worth considering or not for
given i
if A[i]>A[j] and length[i]<length[j]+1
length[i]=length[j]+1
and finally this length array has LIS ending at each
index.
This solution is definitely O(n^2). We can very well solve it in
O(n*logn) time but that method we can not utilize here because we
want to
find LIS ending at each array element and that method will give us
LIS for entire array.
COMMENT DOWN FOR ANY QUERY
PLEASE GIVE A THUIMBS UP
Given an array A[1..n] representing a sequence of n integers, a subsequence is a subset of...
Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...
Please help with #6
'rove: Given a sequence of n2 +1 distinct integers, either there is an increasing subsequence of n+1 terms or a decreasing subsequence of n +1 terms.
'rove: Given a sequence of n2 +1 distinct integers, either there is an increasing subsequence of n+1 terms or a decreasing subsequence of n +1 terms.
Let X = x1, x2, . . . , xn be a sequence of n integers. A sub-sequence of X is a sequence obtained from X by deleting some elements. Give an O(n2) algorithm to find the longest monotonically increasing sub-sequences of X.
(Same-number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers in one line and finds longest subsequence with the same number. Sample Run Enter a series of numbers: 2 4 4 8 8 8 8 2 4 4 0 The longest same number sequence starts at index 3 with 4 values of 8. In Python.
4. Consider the sequence {z,.) such that z1-0, z2-1 and æn-telths,Yn (i) Show that (n) is convergent by showing that the subsequence of odd-indexed terms is monotonic increasing and subsequence of even-indexed terms is monotonic decreasing (ii) Find the limit of {%) (Hint: Consider x,-h-i)
4. Consider the sequence {z,.) such that z1-0, z2-1 and æn-telths,Yn (i) Show that (n) is convergent by showing that the subsequence of odd-indexed terms is monotonic increasing and subsequence of even-indexed terms is monotonic...
Given an array A of integers, -100,000 <= A[i] <= 100,000, 1 <= 10,000 (n = size of array), some elements appear multiple times and others appear once. write a function that returns all the elements that appear multiple times in this array. (solution should be O(n) or faster) public static List<Integer> find duplicates (List<Integer> nums) { }
Let's say you are given a sequence of distinct positive numbers. We want to find a subsequence with the maximum possible sum, with the restriction that we are not allowed to take three consecutive elements from the original sequence. For example, for input 1, 6, 5, 2, 7, 9, 3, 4, the subsequence with the maximum possible sum is 6, 5, 7, 9, 4 (we have two pairs of consecutive elements 6, 5 and 7, 9 but not three consecutive...
Please help with #6
'rove: Given a sequence of n2 +1 distinct integers, either there is an increasing subsequence of n+1 terms or a decreasing subsequence of n +1 terms.
2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words, to select from a sequence of n numbers a subset, including the first number, such that the selected numbers make a longest monotonously increasing sequence. In the exercise b, (i) write an optimal substructure (with an explanation),ii) write the iterative dynamic programming algorithm (pseudocode), and (iii) show the running-time analysis. Hints: the algorithm in the exercise a is a greedy algorithm; the...
Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...