1 / 12

Largest Sum Contiguous Array

Find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. Largest Sum Contiguous Array. Solution. 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].

tryna
Télécharger la présentation

Largest Sum Contiguous Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. Largest Sum Contiguous Array

  2. Solution • 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]

  3. Cut Rod • 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.

  4. Solution • State: cutRod(n)=max(price[i] + cutRod(n-i-1)) for all i in {0,..,n-1}. • Solution cutRod(L)

  5. 0-1 Knapsack • 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).

  6. Solution • 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)

  7. Longest Increasing Subsequence • Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.

  8. Solution • State: S(i) = max(S(k) + 1) where k=0,..,i-1 and a[k]<a[i] • Solution: Max (S(i))

  9. Edit Distance • 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.

  10. Solution • 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)

  11. Topcoder Problem • 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

  12. Example • 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.

More Related