Skip to content

Commit 7e32798

Browse files
refactor 985
1 parent ff9a99f commit 7e32798

File tree

1 file changed

+18
-45
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-45
lines changed
Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,25 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 985. Sum of Even Numbers After Queries
5-
*
6-
* We have an array A of integers, and an array queries of queries.
7-
* For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A.
8-
* (Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)
9-
* Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.
10-
*
11-
* Example 1:
12-
*
13-
* Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
14-
* Output: [8,6,2,4]
15-
* Explanation:
16-
* At the beginning, the array is [1,2,3,4].
17-
* After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
18-
* After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
19-
* After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
20-
* After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
21-
*
22-
* Note:
23-
*
24-
* 1 <= A.length <= 10000
25-
* -10000 <= A[i] <= 10000
26-
* 1 <= queries.length <= 10000
27-
* -10000 <= queries[i][0] <= 10000
28-
* 0 <= queries[i][1] < A.length
29-
*/
303
public class _985 {
31-
public static class Solution1 {
32-
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
33-
int[] result = new int[A.length];
34-
for (int i = 0; i < A.length; i++) {
35-
int col = queries[i][1];
36-
A[col] = A[col] + queries[i][0];
37-
result[i] = computeEvenSum(A);
38-
}
39-
return result;
40-
}
4+
public static class Solution1 {
5+
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
6+
int[] result = new int[A.length];
7+
for (int i = 0; i < A.length; i++) {
8+
int col = queries[i][1];
9+
A[col] = A[col] + queries[i][0];
10+
result[i] = computeEvenSum(A);
11+
}
12+
return result;
13+
}
4114

42-
private int computeEvenSum(int[] A) {
43-
int sum = 0;
44-
for (int num : A) {
45-
if (num % 2 == 0) {
46-
sum += num;
15+
private int computeEvenSum(int[] A) {
16+
int sum = 0;
17+
for (int num : A) {
18+
if (num % 2 == 0) {
19+
sum += num;
20+
}
21+
}
22+
return sum;
4723
}
48-
}
49-
return sum;
5024
}
51-
}
5225
}

0 commit comments

Comments
 (0)