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] < ..., or
nums[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 <= 105
1 <= nums[i] <= 105
1 <= k <= n
Solutions
Solution 1: Dynamic Programming + Binary Indexed Tree
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 - \text{val}\) (reversed, where \(M = \max(\text{nums})\) ), 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 insert the state of index \(i - k\) into the BITs using a sliding pointer.
The time complexity is \(O(n \log M)\) and the space complexity is \(O(M)\), where \(n\) is the length of the array and \(M = \max(\text{nums})\).