|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
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 |
| - */ |
30 | 3 | publicclass_985 {
|
31 |
| -publicstaticclassSolution1 { |
32 |
| -publicint[]sumEvenAfterQueries(int[]A,int[][]queries) { |
33 |
| -int[]result =newint[A.length]; |
34 |
| -for (inti =0;i <A.length;i++) { |
35 |
| -intcol =queries[i][1]; |
36 |
| -A[col] =A[col] +queries[i][0]; |
37 |
| -result[i] =computeEvenSum(A); |
38 |
| - } |
39 |
| -returnresult; |
40 |
| - } |
| 4 | +publicstaticclassSolution1 { |
| 5 | +publicint[]sumEvenAfterQueries(int[]A,int[][]queries) { |
| 6 | +int[]result =newint[A.length]; |
| 7 | +for (inti =0;i <A.length;i++) { |
| 8 | +intcol =queries[i][1]; |
| 9 | +A[col] =A[col] +queries[i][0]; |
| 10 | +result[i] =computeEvenSum(A); |
| 11 | +} |
| 12 | +returnresult; |
| 13 | +} |
41 | 14 |
|
42 |
| -privateintcomputeEvenSum(int[]A) { |
43 |
| -intsum =0; |
44 |
| -for (intnum :A) { |
45 |
| -if (num %2 ==0) { |
46 |
| -sum +=num; |
| 15 | +privateintcomputeEvenSum(int[]A) { |
| 16 | +intsum =0; |
| 17 | +for (intnum :A) { |
| 18 | +if (num %2 ==0) { |
| 19 | +sum +=num; |
| 20 | + } |
| 21 | + } |
| 22 | +returnsum; |
47 | 23 | }
|
48 |
| - } |
49 |
| -returnsum; |
50 | 24 | }
|
51 |
| - } |
52 | 25 | }
|