![]() |
VOOZH | about |
Given an array arr[] which consists of positive integers. Find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order.
Examples:
Input: arr[] = [1, 101, 2, 3, 100]
Output: 106
Explanation: The maximum sum of a increasing sequence is obtained from [1, 2, 3, 100].Input: arr[] = [4, 1, 2, 3]
Output: 6
Explanation: The maximum sum of a increasing sequence is obtained from [1, 2, 3].
Table of Content
This problem is a variation of the standard Longest Increasing Subsequence (LIS) problem. In this problem, we will consider the sum of subsequence instead of its length.
In this approach, we recursively explore all possible subsequences. For each index i, we decide whether to include it in the subsequence based on the last chosen index j. If a[i] > a[j], we have two choices β either include a[i] or skip it and we take the option that gives the maximum sum. If a[i] <= a[j], we cannot include a[i], so we simply move to the next index.
106
In the approach, we observe that many subproblems repeat. For example, while finding the maximum sum of an increasing subsequence starting at index i with the last chosen index j, we repeatedly compute results for the same (i, j) for several states (including or excluding i) across different recursive calls. To avoid recalculating these overlapping subproblems, we store the results of solved states in a DP table and reuse them whenever needed.
106
The main idea is to find the maximum sum increasing subsequence ending at each index i.
To do this, we look at all previous indices j such that arr[j] < arr[i]. This condition ensures that adding arr[i] at the end will keep the subsequence increasing. Among all such valid j, we find the one that gives the maximum sum subsequence ending at j, and then add arr[i] to extend it.
After calculating this for every index, we take the maximum value among all these subsequences β thatβs our final answer.
106
In the above approach, for every element, we look back through all previous elements to find the one with the highest subsequence sum among smaller values. We can observe that it can be optimized if we store-computed results in an ordered data structure that keeps elements sorted by their values. This allows us to quickly find the best result among all smaller elements using a logarithmic lookup, instead of checking each one individually.
106