You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0900-0999/0985.Sum of Even Numbers After Queries/README_EN.md
+59-13
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,13 @@ After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values
60
60
61
61
<!-- solution:start -->
62
62
63
-
### Solution 1
63
+
### Solution 1: Simulation
64
+
65
+
We use an integer variable $\textit{s}$ to record the sum of all even numbers in the array $\textit{nums}$. Initially, $\textit{s}$ is the sum of all even numbers in the array $\textit{nums}$.
66
+
67
+
For each query $(v, i)$, we first check if $\textit{nums}[i]$ is even. If $\textit{nums}[i]$ is even, we subtract $\textit{nums}[i]$ from $\textit{s}$. Then, we add $v$ to $\textit{nums}[i]$. If $\textit{nums}[i]$ is even, we add $\textit{nums}[i]$ to $\textit{s}$, and then add $\textit{s}$ to the answer array.
68
+
69
+
The time complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the arrays $\textit{nums}$ and $\textit{queries}$, respectively. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
0 commit comments