
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
homework 1 homework 1 homework 1
Typology: Assignments
1 / 1
This page cannot be seen from the preview
Don't miss anything!

Assigenment 1
T(i,+1): The length of the longest bumpy subsequence that ends at index i such that the difference a of i - a of i-1 > 0
T(i,−1): The length of the longest bumpy subsequence that ends at index i such that the difference a of i - a of i-1<
For T(i, +1): T(i, +1) = 1 + max(T(k, -1)) for all k < i where a_i > a_k.
For T(i, -1): T(i, -1) = 1 + max(T(k, +1)) for all k < i where a_i < a_k.
The base cases for the recurrence relation will be: T(1, +1) = T(1, -1) = 1.
If there are no preceding elements that satisfy the conditions for a bumpy subsequence, the values are de fined as follows: T(i, +1) = 1 if there are no k < i such that a_i > a_k. T(i, -1) = 1 if there are no k < i such that a_i < a_k.
To find the length of the longest bumpy subsequence in the entire sequence A, we look for the maximum value in all table entries: Length of the longest bumpy subsequence = max(T(i, +1), T(i, -1)) for all i from 1 to n.
If n == 0 return 0 For i from 2 to n: For k from 1 to i-1: If A[i] > A[k]: T_inc[i] = max(T_inc[i], T_dec[k] + 1) Else if A[i] < A[k]: T_dec[i] = max(T_dec[i], T_inc[k] + 1) max_length = 0 For i from 1 to n: max_length = max(max_length, T_inc[i], T_dec[i])
Return max_length
starting from the top first loop runtime O(n) and inside it another loop of also runtime O(n) inside the nested loop if else statments with runtime O(1) the total runtime of the nested loops is O(n^2) then another loop of runtime O(n) the final runtime of the program comes to O(n^2).