-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Python solution for "985. Sum of Even Numbers After Queries"
- Loading branch information
1 parent
e0a2a22
commit 4a817b7
Showing
2 changed files
with
181 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]])) |
Oops, something went wrong.