Beats 100.00%💯💯 Easy to understand 🔥🔥
Approach\n\n\n---\n\n\n#### Initialization:\n1. Define Constants and Variables:*\n\n Set MOD to 1000000007 for handling large numbers.\n Initialize sum to 0 to 
# Approach\n\n\n---\n\n\n#### **Initialization:**\n**1. **Define Constants and Variables:****\n\n* Set MOD to 1000000007 for handling large numbers.\n* Initialize sum to 0 to accumulate the results of each query.\n* Initialize a dp array of size n to store the maximum sum of non-adjacent subsequences ending at each index.\n\n**2. **Compute Initial dp Array:****\n\ndp[0] = Math.max(0, nums[0]): The maximum sum ending at the first element is either 0 (not choosing it) or nums[0].\nIf n > 1, dp[1] = Math.max(dp[0], nums[1]): For the second element, the maximum is either the sum ending at the first element or nums[1].\nFor each i from 2 to n-1, compute dp[i]:\ndp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]): Either exclude the current element or include it by adding it to the best sum ending two elements before.\n\n---\n\n- #### Process Queries:\n**3. Iterate Over Each Query:**\n\n* For each query [pos, value]:\n     --> Update nums[pos] to value.\n\n**4.  Recompute dp Array from Affected Position:**\n\n* If pos == 0:\n   -->  Recompute dp[0] = Math.max(0, nums[0]).\n--> If n > 1, recompute dp[1] = Math.max(dp[0], nums[1]).\n- If pos == 1:\nRecompute dp[1] = Math.max(dp[0], nums[1]).\n\n**5. Update Remaining dp Values:**\n\n* For each i from Math.max(2, pos) to n-1, recompute dp[i]:\n-->dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]).\n\n**6.Update the Sum:**\n\nAdd the current maximum sum (dp[n-1]) to sum and take modulo MOD:\nsum = (sum + dp[n - 1]) % MOD.\n\n---\n\n#### Return the Result:\n**7. Return the Accumulated Sum**:\nReturn sum cast to an integer.\n# Complexity\n - Time complexity: **O(Q*N)**,  where N is the length of the input array and Q is the number of queries.\n- Space complexity: **O(N)**, primarily due to the dp array.\n\n# Code\n```\nclass Solution {\n   private static final int MOD = 1000000007;\n\n    public int maximumSumSubsequence(int[] nums, int[][] queries) {\n         int n = nums.length;\n        long sum = 0;\n        long[] dp = new long[n];\n\n        dp[0] = Math.max(0, nums[0]);\n        if (n > 1) {\n            dp[1] = Math.max(dp[0], nums[1]);\n        }\n        for (int i = 2; i < n; i++) {\n            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);\n        }\n\n        for (int[] q : queries) {\n            int pos = q[0];\n            int value = q[1];\n            nums[pos] = value;\n\n            if (pos == 0) {\n                dp[0] = Math.max(0, nums[0]);\n                if (n > 1) {\n                    dp[1] = Math.max(dp[0], nums[1]);\n                }\n            } else if (pos == 1) {\n                dp[1] = Math.max(dp[0], nums[1]);\n            }\n            for (int i = Math.max(2, pos); i < n; i++) {\n                dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);\n            }\n\n            sum = (sum + dp[n - 1]) % MOD;\n        }\n\n        return (int) sum;\n    }\n}\n```\n![wkjfjeb.jpg](https://assets.leetcode.com/users/images/d084d081-3ffc-434d-a236-ab001541be0d_1716707208.4513226.jpeg)\n
['Dynamic Programming', 'Python', 'Java']