This is the first book of pleasure and delight 370 update, the first 398 Pian original
01 questions and look ready
Introduced today is the LeetCode
arithmetic problem in Easy
the first-level 232
title (overall title number is 985
). There are an array of integers A and a query array of queries.
For the i-th query val = queries[i][0]
, index = queries[i][1]
we will val
add to A[index]
. Then, the i
answer to the query is A
the sum of the even value. (Here given index = queries[i][1]
is a zero-based indexing, each query will modify the array A
.)
Return all the answers queries. Your answer array answer
should be answer[i]
used as the first i
query answer. E.g:
Input: A = [1,2,3,4], queries = [[1,0], [- 3,1], [- 4,0], [2,3]]
Output: [8,6, 2,4]
Description: In the beginning, the array is [1,2,3,4].
In the A[0]
after adding 1, the array is [2,2,3,4], and the even values of 2 + 2 + 4 = 8.
In the A[1]
after adding -3 array is [2, 3,4], the sum is an even value of 2 + 4 = 6.
In the A[0]
after adding -4 array is [-2, -1,3,4], and the even values of -2 + 2 = 4.
In the A[3]
after adding 2, the array is [-2, 1,3,6], and the even values of -2 + 4 = 6.
Note :
1 <= A.length <= 10000
-10000 <= A[i] <= 10000
1 <= queries.length <= 10000
-10000 <= queries[i][0] <= 10000
0 <= queries[i][1] <A.length
02 The first solution
Direct translation problem, to find the queries
corresponding index and val
, changing the value of the corresponding element of A, followed by cyclic find A
the sum of the even elements, assigned to the resulting array result
.
Solution of this time complexity is O(M*N)
, M
of queries
the length N
of A
the length, the spatial complexity O(M)
, M
for the queries
length.
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
int len = queries.length;
int[] result = new int[len];
for (int i=0; i<len; i++) {
A[queries[i][1]] += queries[i][0];
result[i] = evenSum(A);
}
return result;
}
public int evenSum(int[] A){
int sum = 0;
for (int num : A) {
if (num%2==0) {
sum += num;
}
}
return sum;
}
03 The second solution
The time complexity of the first solution is too high, can then Optimization.
Result values in the array, is after a previous generation on the basis of one of the main determining A
the current bit element values, queries中
values of parity . Take for example the title of view, after the second operation, A
become [2,-1,3,4]
, after the first operation of the array A=[2,2,3,4]
of the second element becomes -1, and even elements of the first operation and 8 are now the second element becomes odd, it is necessary to first add 2 to lose, the operation of the second even and become 8-2 = 6, the remaining steps of the operation can be understand this.
Therefore, we need to determine the current operation of A[i]
the parity and queries[i][0]
the parity can be divided into four cases:
First case : queries[i][0]
is an even number, A[i]
but also for an even number, i.e., there is a summation of the front A[i]
, just add queries[i][0]
value to, ie sum = sum + queries[i][0]
.
The second case : queries[i][0]
even, A[i]
odd, that is a sum of the former does not A[i]
, and A[i]加上queries[i][0]
later also odd, so do not update sum
values.
The third case : queries[i][0]
an odd number, A[i]
an even number, i.e., there is the previous summation A[i]
, now A[i]
adding queries[i][0]
the odd into, needs to be the summation of the previous A[i]
subtracted, i.e. sum = sum - A[i]
.
The fourth case : queries[i][0]
odd, A[i]
also an odd number, i.e. without a previous summation A[i]
, but A[i]
adding queries[i][0]
the became even, even need to put this new added sum
, namely sum = sum + queries[i][0] + A[i]
.
After calculating sum
the value of the sum is assigned to the position corresponding to the new array element, ueries[i][0] + A[i]
the value assigned A[i]
, and returns the result array.
Solution of this time complexity is O(M)
, M
of queries
the length of the spatial complexity O(M)
, M
for the queries
length.
public int[] sumEvenAfterQueries2(int[] A, int[][] queries) {
int sum = 0, i = 0;
for (int num : A) {
if (num%2 == 0) {
sum += num;
}
}
int[] result = new int[queries.length];
for (int[] arr : queries) {
int curval = arr[0];
int preval = A[arr[1]];
// 做奇偶判断
if (curval%2 == 0) {
if (preval%2 == 0) {
sum = sum + curval;
}
} else {
if (preval%2 == 0) {
sum = sum - preval;
} else {
sum = sum + curval + preval;
}
}
A[arr[1]] += curval;
result[i++] = sum;
}
return result;
}
04 Summary
Thematic algorithm has been continuously day and more than seven months , the algorithm of feature articles 238 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures ] either a keyword to obtain a series of articles Collection .
That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!