Skip to content

Commit

Permalink
Added Python solution for "985. Sum of Even Numbers After Queries"
Browse files Browse the repository at this point in the history
  • Loading branch information
chemandante committed Oct 4, 2022
1 parent e0a2a22 commit 4a817b7
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 149 deletions.
31 changes: 31 additions & 0 deletions 09/85/m985.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Solved:
# (M) Sum of Even Numbers After Queries
# https://leetcode.com/problems/sum-of-even-numbers-after-queries/

from typing import List


class Solution:
def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:
res = [0] * len(queries)
s = sum([n for n in nums if n % 2 == 0])

for i, row in enumerate(queries):
n = nums[row[1]]
# If next number to be replaced was even, subtract its value from sum
if n & 1 == 0:
s -= n
n += row[0]
# If new value is even, add it to sum
if n & 1 == 0:
s += n

nums[row[1]] = n
res[i] = s

return res


x = Solution()
print(x.sumEvenAfterQueries(nums=[1, 2, 3, 4], queries=[[1, 0], [-3, 1], [-4, 0], [2, 3]]))
print(x.sumEvenAfterQueries(nums=[1], queries=[[4, 0]]))
Loading

0 comments on commit 4a817b7

Please sign in to comment.