We have an array A of integers, and an array queries of queries.
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.
(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)
Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.
Example 1:
Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
Explanation:
At the beginning, the array is [1,2,3,4].
After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
Note:
1 <= A.length <= 10000
-10000 <= A[i] <= 10000
1 <= queries.length <= 10000
-10000 <= queries[i][0] <= 10000
0 <= queries[i][1] < A.length
Solution: Simulation
Time complexity: O(n + |Q|) Space complexity: O(n)
On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).
Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.
You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?
Example 1:
Input: [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.
Example 2:
Input: [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
Output: 16
Explanation: 0 1 2 3 4
24 23 22 21 512 13 14 15 1611 17 18 19 20
10 9 8 7 6
The final route is marked in bold.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
Note:
2 <= N <= 50.
grid[i][j] is a permutation of [0, …, N*N – 1].
Solution 1: Dijkstra’s Algorithm
Time complexity: O(n^2*logn) Space complexity: O(n^2)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Author: Huahua, running time: 8 ms
classSolution{
public:
intswimInWater(vector<vector<int>>&grid){
constintn=grid.size();
priority_queue<pair<int,int>>q;// {-time, y * N + x}
q.push({-grid[0][0],0*n+0});
vector<int>seen(n*n);
vector<int>dirs{-1,0,1,0,-1};
seen[0*n+0]=1;
while(!q.empty()){
auto node=q.top();q.pop();
intt=-node.first;
intx=node.second%n;
inty=node.second/n;
if(x==n-1&&y==n-1)returnt;
for(inti=0;i<4;++i){
inttx=x+dirs[i];
intty=y+dirs[i+1];
if(tx<0||ty<0||tx>=n||ty>=n)continue;
if(seen[ty*n+tx])continue;
seen[ty*n+tx]=1;
q.push({-max(t,grid[ty][tx]),ty*n+tx});
}
}
return-1;
}
};
Solution 2: Binary Search + BFS
Time complexity: O(2logn * n^2) Space complexity: O(n^2)
Segment tree is a balanced binary tree with O(logn) height given n input segments. Segment tree supports fast range query O(logn + k), and update O(logn). Building such a tree takes O(n) time if the input is an array of numbers.