Skip to content

Commit 86d1d47

Browse files
add 985
1 parent 8e1b24f commit 86d1d47

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|985|[Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_985.java) | O(n) | O(n) | |Easy| Array
3233
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_977.java) | O(nlogn) | O(1) | |Easy| Array
3334
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_976.java) | O(nlogn) | O(1) | |Easy| Math Array
3435
|974|[Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_974.java) | O(n) | O(n) | |Medium| Array|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
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+
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+
}
41+
42+
private int computeEvenSum(int[] A) {
43+
int sum = 0;
44+
for (int num : A) {
45+
if (num % 2 == 0) {
46+
sum += num;
47+
}
48+
}
49+
return sum;
50+
}
51+
}
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._985;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _985Test {
10+
private static _985.Solution1 solution1;
11+
private static int[] expected;
12+
private static int[] actual;
13+
private static int[] A;
14+
private static int[][] queries;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _985.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
A = new int[] {1, 2, 3, 4};
24+
queries = new int[][] {{1, 0}, {-3, 1}, {-4, 0}, {2, 3}};
25+
expected = new int[] {8, 6, 2, 4};
26+
actual = solution1.sumEvenAfterQueries(A, queries);
27+
assertArrayEquals(expected, actual);
28+
}
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy