Skip to content

Commit 91114eb

Browse files
committed
added problem 985
1 parent fa8a7bb commit 91114eb

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

problems/problem0985/even_query.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem0985
2+
3+
/*
4+
You are given an integer array nums and an array queries where queries[i] = [vali, indexi].
5+
For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.
6+
Return an integer array answer where answer[i] is the answer to the ith query.
7+
*/
8+
9+
func sumEvenAfterQueries(nums []int, queries [][]int) []int {
10+
var sum int
11+
var sums = make([]int, len(queries))
12+
// Getting initial sum
13+
for i := range nums {
14+
if nums[i]%2 == 0 {
15+
sum += nums[i]
16+
}
17+
}
18+
for i := range queries {
19+
old := nums[queries[i][1]]
20+
new := old + queries[i][0]
21+
// If the old value is even, we want to
22+
// delete it from the sum
23+
if old%2 == 0 {
24+
sum -= old
25+
}
26+
// If the new value is even, we want to
27+
// add it to the sum
28+
if new%2 == 0 {
29+
sum += new
30+
}
31+
nums[queries[i][1]] = new
32+
sums[i] = sum
33+
}
34+
return sums
35+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem0985
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []int
12+
Queries [][]int
13+
Expected []int
14+
}
15+
16+
var Results = []Result{
17+
{[]int{1, 2, 3, 4}, [][]int{{1, 0}, {-3, 1}, {-4, 0}, {2, 3}}, []int{8, 6, 2, 4}},
18+
{[]int{1}, [][]int{{4, 0}}, []int{0}},
19+
}
20+
21+
func TestEvenSumAfterQueries(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, res := range Results {
25+
want := res.Expected
26+
got := sumEvenAfterQueries(res.Input, res.Queries)
27+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
28+
}
29+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Each problem is in it's own directory, with test files. There are helper package
144144
| 0967 | [Numbers With Same Consecutive Differences](https://leetcode.com/problems/numbers-with-same-consecutive-differences) | [My Solution](./problems/problem0967) ||
145145
| 0968 | [Binary Tree Cameras](https://leetcode.com/problems/binary-tree-cameras) | [My Solution](./problems/problem0968) ||
146146
| 0977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array) | [My Solution](./problems/problem0977) ||
147+
| 0985 | [Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries) | [My Solution](./problems/problem0985) ||
147148
| 0987 | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree) | [My Solution](./problems/problem0987) ||
148149
| 0994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges) | [My Solution](./problems/problem0994) ||
149150
| 1009 | [Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer) | [My Solution](./problems/problem1009) ||

0 commit comments

Comments
 (0)