Leetcode 1027 Solution
This article provides solution to leetcode question 1027 (sum-of-even-numbers-after-queries)
Access this page by simply typing in "lcs 1027" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/sum-of-even-numbers-after-queries
Solution
class Solution:
def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:
ans = []
even_sum = sum([a for a in A if a % 2 == 0])
for val, index in queries:
old_a = A[index]
new_a = A[index] + val
if old_a % 2 == 0:
even_sum -= old_a
if new_a % 2 == 0:
even_sum += new_a
ans.append(even_sum)
A[index] = new_a
return ans