3915. Maximum Sum of Alternating Subsequence With Distance at Least K
SourceWeekly Contest 499 Q4DifficultyHardRating2288
Description
You are given an integer array nums of length n and an integer k.
Pick a subsequence with indices 0 <= i1 < i2 < ... < im < n such that:
- For every
1 <= t < m,it+1 - it >= k. - The selected values form a strictly alternating sequence. In other words, either:
nums[i1] < nums[i2] > nums[i3] < ..., ornums[i1] > nums[i2] < nums[i3] > ...
A subsequence of length 1 is also considered strictly alternating. The score of a valid subsequence is the sum of its selected values.
Return an integer denoting the maximum possible score of a valid subsequence.
Example 1:
Input: nums = [5,4,2], k = 2
Output: 7
Explanation:
An optimal choice is indices [0, 2], which gives values [5, 2].
- The distance condition holds because
2 - 0 = 2 >= k. - The values are strictly alternating because
5 > 2.
The score is 5 + 2 = 7.
Example 2:
Input: nums = [3,5,4,2,4], k = 1
Output: 14
Explanation:
An optimal choice is indices [0, 1, 3, 4], which gives values [3, 5, 2, 4].
- The distance condition holds because each pair of consecutive chosen indices differs by at least
k = 1. - The values are strictly alternating since
3 < 5 > 2 < 4.
The score is 3 + 5 + 2 + 4 = 14.
Example 3:
Input: nums = [5], k = 1
Output: 5
Explanation:
The only valid subsequence is [5]. A subsequence with 1 element is always strictly alternating, so the score is 5.
Constraints:
1 <= n == nums.length <= 1051 <= nums[i] <= 1051 <= k <= n
Solutions
Solution 1: Dynamic Programming + Binary Indexed Tree
Thinking
A subsequence DP that enumerates every predecessor at least \(k\) away is \(O(n^2)\) and fails for \(n\le 10^5\). Transfers also require the predecessor to be larger or smaller.
Let \(f[i][0]\) be the best alternating subsequence ending at \(i\) as a valley, and \(f[i][1]\) as a peak. A valley may follow only a larger peak, a peak only a smaller valley, and the indices must differ by at least \(k\).
Two Fenwick trees store prefix maxima of \(f[\cdot][0]\) and suffix maxima of \(f[\cdot][1]\) over the discretized value domain. At index \(i\) we first insert the state at \(i-k\), then query; every predecessor used in a transition is then at least \(k\) away.
State Definition
Let \(f[i][0]\) denote the maximum sum of a valid subsequence ending at index \(i\) where the last element is a valley (the next element must be larger to maintain alternation), and \(f[i][1]\) denote the maximum sum where the last element is a peak (the next element must be smaller).
Transitions
When transitioning, we enumerate a predecessor index \(j\) satisfying \(j \leq i - k\):
- State \(f[i][0]\) (valley): transitions from \(f[j][1]\), requiring \(\text{nums}[j] > \text{nums}[i]\), i.e., query the maximum \(f[\cdot][1]\) over the value range \((\text{nums}[i],\ +\infty)\):
- State \(f[i][1]\) (peak): transitions from \(f[j][0]\), requiring \(\text{nums}[j] < \text{nums}[i]\), i.e., query the maximum \(f[\cdot][0]\) over the value range \([1,\ \text{nums}[i]-1]\):
The final answer is \(\max_{0 \leq i < n}\max(f[i][0],\ f[i][1])\).
Optimization
The transitions involve dynamic prefix/suffix maximum queries over a value domain, which can be maintained efficiently with two Binary Indexed Trees (BITs):
- BIT \(\text{bit}_0\): indexed by value, maintains the prefix maximum of \(f[\cdot][0]\), used to query cases where \(\text{nums}[j] < \text{nums}[i]\).
- BIT \(\text{bit}_1\): indexed by \(m + 1 - \textit{rank}\) (reversed, where \(m\) is the number of distinct values), maintains the prefix maximum of \(f[\cdot][1]\), equivalent to a suffix maximum over the value domain, used to query cases where \(\text{nums}[j] > \text{nums}[i]\).
To ensure only indices \(j \leq i - k\) participate in transitions, when processing index \(i\), we first insert the state of index \(i - k\) into the BITs, then query.
The time complexity is \(O(n \log m)\) and the space complexity is \(O(n + m)\), where \(n\) is the length of the array and \(m\) is the number of distinct values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |