2672.cpp (845B)
1 class Solution { 2 public: 3 vector<int> colorTheArray(const int n, const vector<vector<int>> &queries) const { 4 static int nums[100002]; 5 memset(nums, 0x00, sizeof(nums)); 6 7 const int m = size(queries); 8 vector<int> res(m); 9 10 int crnt = 0; 11 for (int i = 0; i < m; i++) { 12 const int idx = queries[i][0] + 1; 13 const int color = queries[i][1]; 14 if (nums[idx] != color) { 15 if (nums[idx]) { 16 if (nums[idx] == nums[idx - 1]) crnt--; 17 if (nums[idx] == nums[idx + 1]) crnt--; 18 } 19 nums[idx] = color; 20 if (nums[idx] == nums[idx - 1]) crnt++; 21 if (nums[idx] == nums[idx + 1]) crnt++; 22 } 23 res[i] = crnt; 24 } 25 26 return res; 27 } 28 };