3165. Maximum Sum of Subsequence With Non-adjacent Elements
3165. Maximum Sum of Subsequence With Non-adjacent Elements
Description
You are given an array nums
consisting of integers. You are also given a 2D array queries
, where queries[i] = [posi, xi].
For query i
, we first set nums[posi] equal to xi, then we calculate the answer to query i
which is the maximum sum of a subsequence of nums
where no two adjacent elements are selected .
Return the sum of the answers to all queries.
Since the final answer may be very large, return it modulo 10^9 + 7
.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
1 | Input: nums = [3,5,9], queries = [[1,-2],[0,-3]] |
Example 2:
1 | Input: nums = [0,-1], queries = [[0,-5]] |
Constraints:
1 <= nums.length <= 5 * 10^4
-10^5 <= nums[i] <= 10^5
1 <= queries.length <= 5 * 10^4
queries[i] == [pos<sub>i</sub>, x<sub>i</sub>]
0 <= pos<sub>i</sub> <= nums.length - 1
-10^5 <= x<sub>i</sub> <= 10^5
Hints/Notes
- 2024/03/02
- Segment Tree
- if a problem can be solved with divide and conquer, then the modified version to change value can be solved with segment tree
- 0x3Fâs solution
- Weekly Contest 399
Solution
Language: C++
1 | class Solution { |