-
Notifications
You must be signed in to change notification settings - Fork 3
/
2672.cpp
35 lines (34 loc) · 1.14 KB
/
2672.cpp
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
29
30
31
32
33
34
35
class Solution {
public:
vector<int> colorTheArray(int n, vector<vector<int>>& queries) {
int m = queries.size();
vector<int> res(m, 0);
if (m == 1) return res;
vector<int> nums(n, 0);
int count = 0;
for (int i = 0; i < m; ++i) {
int index = queries[i][0];
int color = queries[i][1];
int currentColor = nums[index];
if (index > 0) {
int preColor = nums[index - 1];
if (currentColor == preColor && currentColor != 0) count--;
}
if (index < n - 1) {
int postColor = nums[index + 1];
if (currentColor == postColor && currentColor != 0) count--;
}
nums[index] = color;
if (index > 0) {
int preColor = nums[index - 1];
if (color == preColor && color != 0) count++;
}
if (index < n - 1) {
int postColor = nums[index + 1];
if (color == postColor && color != 0) count++;
}
res[i] = count;
}
return res;
}
};