Skip to content

Commit 73d2a09

Browse files
committed
Add solution #985
1 parent 78714f8 commit 73d2a09

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 985. Sum of Even Numbers After Queries
3+
* https://leetcode.com/problems/sum-of-even-numbers-after-queries/
4+
* Difficulty: Easy
5+
*
6+
* We have an array A of integers, and an array queries of queries.
7+
*
8+
* For the i-th query val = queries[i][0], index = queries[i][1],
9+
* we add val to A[index]. Then, the answer to the i-th query
10+
* is the sum of the even values of A.
11+
*
12+
* (Here, the given index = queries[i][1] is a 0-based index, and
13+
* each query permanently modifies the array A.)
14+
*
15+
* Return the answer to all queries. Your answer array should
16+
* have answer[i] as the answer to the i-th query.
17+
*/
18+
19+
/**
20+
* @param {number[]} A
21+
* @param {number[][]} queries
22+
* @return {number[]}
23+
*/
24+
var sumEvenAfterQueries = function(A, queries) {
25+
return queries.reduce((sums, query) => {
26+
A[query[1]] += query[0];
27+
return [...sums, A.reduce((sum, n) => sum + (n % 2 === 0 ? n : 0), 0)];
28+
}, []);
29+
};

0 commit comments

Comments
 (0)