CareerCup Find a subarray
2014-02-21 21:31
316 查看
Given an array of pairs of the form <a, b>. We have to find a sub-array such that the 1st element in the pairs are in increasing
order and the sum of 2nd element of the pairs in the sub-array is maximum possible
--------------------------------------------------------------------------------------------------------
Do you know what does subarray means ? It's continuous! So DP is enough like this:
If the sub-sequence is not continuous, it's similar to longest increasing sub-sequence.
order and the sum of 2nd element of the pairs in the sub-array is maximum possible
--------------------------------------------------------------------------------------------------------
Do you know what does subarray means ? It's continuous! So DP is enough like this:
int max_ending_here, max_ending_so_far = 0; int begin, end = 0; int begin_temp = begin; for(int i = 1; i< pairsArray.length; i++) { if(pairsArray[i-1].first < pairsArray[i].first) { if(max_ending_here < 0) { max_ending_here = pairsArray[i].second; begin_temp = i; } else { max_ending_here += pairsArray[i].second; } if(max_ending_here >= max_ending_so_far) { max_ending_so_far = max_ending_here; begin = begin_temp; end = i; } } else { max_ending_here = 0; begin_temp = i; } }
If the sub-sequence is not continuous, it's similar to longest increasing sub-sequence.
相关文章推荐
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- CareerCup Find all the conflicting appointments from a given list of n appointments.
- CareerCup Find at-least one triplet such that A[n-1] >= A[n] <= A[n+1].
- CareerCup Find the ceiling value present in the BST of a given ke
- CareerCup Write the code to find lexicographic minimum in a circular array
- CareerCup Given a sorted array which contains scores. Write a program to find occurrence
- CareerCup Find the representation as a tree with the least height
- CareerCup Find if an array is a sequence
- CareerCup Find the usernames existing in both documents
- CareerCup Find the smallest range that includes at least one number from each of the k sorted lists.
- CareerCup Find the biggest interval that has all its members in list in O(n)
- CareerCup Find the no. of expressions that evaluate to a Walprime
- CareerCup Find whether a given number is a perfect square or not.(Addition and Subtraction only)
- CareerCup Find the diameter of the tree
- CareerCup How to find medium of 1 billion numbers across N distributed machines efficiently?
- CareerCup Find top k values (asec) which can either be the number from the array A
- CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
- CareerCup Find out the winning probability given n, m and x
- Moderate 猜测序列hit或是pseudo-hit @CareerCup
- Moderate 最大连续序列之和 @CareerCup