Slides

advertisement
Find the sum of contiguous subarray within a
one-dimensional array of numbers which has
the largest sum.
State:
Sum(i) = Sum(i-1) + a[i] if Sum(i-1) + a[i]>0
=0
otherwise

Solution:
Max ( Sum(i) )

i Є [0,n-1]


Given a rod of length n inches and an array
of prices that contains prices of all pieces of
size smaller than n
Determine the maximum value obtainable
by cutting up the rod and selling the pieces.

State:
cutRod(n)=max(price[i] + cutRod(n-i-1))
for all i in {0,..,n-1}.

Solution
cutRod(L)


Given weights and values of n items, put
these items in a knapsack of capacity W to
get the maximum total value in the
knapsack.
You cannot break an item, either pick the
complete item, or don’t pick it (0-1
property).
State:
C(i,j) = max( C(i-1,j-w[i]) + v[i] , C(i-1,j) )

(Note : Check for j-w[i]>=0)
Solution:
C(n,W)


Find the length of the longest subsequence
of a given sequence such that all elements
of the subsequence are sorted in increasing
order.
State:
S(i) = max(S(k) + 1)
a[k]<a[i]

Solution:
Max (S(i))

where k=0,..,i-1 and


Given two strings of size m, n and set of
operations replace (R), insert (I) and delete
(D) all at equal(or different) cost.
Find minimum number of edits (operations)
required to convert one string into another.
State
E(i, j) = min( E(i-1, j) + D,
•
E(i, j-1) + I,
E(i-1, j-1) + R if S[i]!=S[j]
E(i-1,j-1) + 0 if S[i]=S[j] )
Solution
E(n,m)
•



Given a string composed of lowercase
alphabets 'a'-'z' and '?'.
replace every '?' in S with a random
lowercase letter ('a' – 'z') – uniform
probability
Find expected number of palindromic
substrings in S
Test Case 1
 aaa
6
Test Case 2
 z??
 3.11
There are 26^2 = 676 equally likely possibilities for the letters used to replace
the question marks. Here are all possible outcomes:

The string "zzz" has 6 palindromic substrings.

Each of the 25 strings "zaz", "zbz", ..., "zyz" has 4 palindromic substrings.

Each of the 25 strings "zza", "zzb", ..., "zzy" has 4 palindromic substrings.

Each of the 25 strings "zaa", "zbb", ..., "zyy" has 4 palindromic substrings.

Each of the remaining 600 possible strings only has the 3 single-letter
palindromic substrings.

The expected number of palindromic substrings can be computed simply as
the average over all 676 possible cases. Hence, the correct return value is (6 +
75*4 + 600*3) / 676.
Download