0

I know there is a longest increasing subsequence algorithm that runs in O(nlogn) (https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/), but what if there are multiple increasing subsequences of the same length and I want to take the one with the minimum sum? Is there a way to calculate this, in O(nlogn) as well? I have found one that does it, but in (n^2) (https://www.geeksforgeeks.org/maximize-sum-of-all-elements-which-are-not-a-part-of-the-longest-increasing-subsequence/).

  • 1
    You should be able to keep the sum of the longest sequence found so far and calculate the sum of the current sequence. Only make the new sequence into the chosen sequence if it is longer than the previous longest or if it is the same length but has a smaller sum. Presumably, the first number in the array is the initial 'longest sequence' with length 1 and the obvious value; that gets you past the problems with initialization. (If there are no entries in the array, there is no longest increasing subsequence.). Th extra work doesn't change the complexity of the algorithm; it remains O(nlogn). – Jonathan Leffler May 13 '22 at 17:10
  • You might look at https://cs.stackexchange.com/questions/7409/count-unique-increasing-subsequences-of-length-3-in-on-log-n for inspiration. That community might also be better suited for this kind of question. – Franck May 13 '22 at 21:05

0 Answers0