3640. Trionic Array II
Description
You are given an integer array nums of length n.
A trionic subarray is a contiguous subarray nums[l...r] (with 0 <= l < r < n) for which there exist indices l < p < q < r such that:
nums[l...p]is strictly increasing,nums[p...q]is strictly decreasing,nums[q...r]is strictly increasing.
Return the maximum sum of any trionic subarray in nums.
Example 1:
Input: nums = [0,-2,-1,-3,0,2,-1]
Output: -4
Explanation:
Pick l = 1, p = 2, q = 3, r = 5:
nums[l...p] = nums[1...2] = [-2, -1]is strictly increasing (-2 < -1).nums[p...q] = nums[2...3] = [-1, -3]is strictly decreasing (-1 > -3)nums[q...r] = nums[3...5] = [-3, 0, 2]is strictly increasing (-3 < 0 < 2).- Sum =
(-2) + (-1) + (-3) + 0 + 2 = -4.
Example 2:
Input: nums = [1,4,2,7]
Output: 14
Explanation:
Pick l = 0, p = 1, q = 2, r = 3:
nums[l...p] = nums[0...1] = [1, 4]is strictly increasing (1 < 4).nums[p...q] = nums[1...2] = [4, 2]is strictly decreasing (4 > 2).nums[q...r] = nums[2...3] = [2, 7]is strictly increasing (2 < 7).- Sum =
1 + 4 + 2 + 7 = 14.
Constraints:
4 <= n = nums.length <= 105-109 <= nums[i] <= 109- It is guaranteed that at least one trionic subarray exists.
Solutions
Solution 1: Grouped Loop
Thinking
We want the maximum-sum trionic subarray. Enumerating peaks is quadratic. Adjacent trionic pieces share an ascent, so a grouped scan lists maximal ones in linear time.
A pointer cuts a rise, a fall, and a rise; a degenerate middle or end is skipped. The sum of a maximal piece is a fixed middle plus the best leftward suffix of the first rise and the best rightward prefix of the last rise.
The third ascent can start the next piece, so the pointer rewinds to the valley \(q\). Each index is visited a constant number of times.
We can traverse the array to find all possible maximal trionic subarrays, calculate their sums, and update the maximum value.
We define a pointer \(i\), initially \(i = 0\), representing the current position pointing to the first element of the array. We move \(i\) to the right until we find the first element that does not satisfy strict increase, i.e., \(nums[i-1] \geq nums[i]\). If at this point \(i = l + 1\), it means this segment has only one element and cannot form an increasing sequence, so we continue to the next iteration.
Next, we define pointer \(p\), representing the end position of the current increasing segment. Then we find the second strictly decreasing part. If this segment has only one element, or reaches the end of the array, or encounters equal elements, we continue to the next iteration.
Then we define pointer \(q\), representing the end position of the current decreasing segment. Next, we find the third strictly increasing part. At this point, we have found a maximal trionic subarray. The maximum sum of this trionic subarray consists of the following parts:
- The sum of elements in the index range \([p-2,..,q+1]\)
- The sum of the maximum increasing subarray extending left from \(p-3\), or 0 if it doesn't exist
- The sum of the maximum increasing subarray extending right from \(q+2\), or 0 if it doesn't exist.
After calculating the sum of this trionic subarray, we update the answer. Then we move pointer \(i\) to position \(q\), because the increasing part of the third segment can serve as the first increasing segment of the next iteration.
After the traversal is complete, we return the answer.
The time complexity is \(O(n)\), where \(n\) is the length of the array. The space complexity is \(O(1)\), using only constant-level extra space.
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |