Skip to content

Latest commit

 

History

History
88 lines (50 loc) · 2.26 KB

3165-maximum-sum-of-subsequence-with-non-adjacent-elements.adoc

File metadata and controls

88 lines (50 loc) · 2.26 KB

3165. Maximum Sum of Subsequence With Non-adjacent Elements

{leetcode}/problems/maximum-sum-of-subsequence-with-non-adjacent-elements/[LeetCode - 3165. Maximum Sum of Subsequence With Non-adjacent Elements ^]

You are given an array nums consisting of integers. You are also given a 2D array queries, where queries[i] = [pos<sub>i</sub>, x<sub>i</sub>].

For query i, we first set nums[pos<sub>i</sub>] equal to x<sub>i</sub>, then we calculate the answer to query i which is the maximum sum of a <span data-keyword="subsequence-array">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 109 + 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:

<div class="example-block"> Input: <span class="example-io">nums = [3,5,9], queries = [[1,-2],[0,-3]]

Output: <span class="example-io">21

Explanation:

After the 1st query, nums = [3,-2,9] and the maximum sum of a subsequence with non-adjacent elements is 3 + 9 = 12.

After the 2nd query, nums = [-3,-2,9] and the maximum sum of a subsequence with non-adjacent elements is 9.

Example 2:

<div class="example-block"> Input: <span class="example-io">nums = [0,-1], queries = [[0,-5]]

Output: <span class="example-io">0

Explanation:

After the 1st query, nums = [-5,-1] and the maximum sum of a subsequence with non-adjacent elements is 0 (choosing an empty subsequence).

Constraints:

  • 1 ⇐ nums.length ⇐ 5 * 104

  • -105 ⇐ nums[i] ⇐ 105

  • 1 ⇐ queries.length ⇐ 5 * 104

  • queries[i] == [pos<sub>i</sub>, x<sub>i</sub>]

  • 0 ⇐ pos<sub>i</sub> ⇐ nums.length - 1

  • -105 ⇐ x<sub>i</sub> ⇐ 105

思路分析

一刷
link:{sourcedir}/_3165_MaximumSumOfSubsequenceWithNonAdjacentElements.java[role=include]

参考资料