RainX_69

Number of Adjacent Elements With the Same Color | HARD | GREEDY | OA | MUST DO

May 8th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | Source Code | 0 0
  1. https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/
  2.  
  3. There is a 0-indexed array nums of length n. Initially, all elements are uncolored (has a value of 0).
  4. You are given a 2D integer array queries where queries[i] = [indexi, colori].
  5. For each query, you color the index indexi with the color colori in the array nums.
  6. Return an array answer of the same length as queries where answer[i] is the number of adjacent elements with the same color after the ith query.
  7. More formally, answer[i] is the number of indices j, such that 0 <= j < n - 1 and nums[j] == nums[j + 1] and nums[j] != 0 after the ith query.
  8.  
  9. Example 1:
  10. Input: n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
  11. Output: [0,1,1,0,2]
  12. Explanation: Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
  13. - After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
  14. - After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
  15. - After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
  16. - After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
  17. - After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.
  18.  
  19. Example 2:
  20. Input: n = 1, queries = [[0,100000]]
  21. Output: [0]
  22. Explanation: Initially array nums = [0], where 0 denotes uncolored elements of the array.
  23. - After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0.
  24.  
  25.  
  26. Constraints:
  27. 1 <= n <= 105
  28. 1 <= queries.length <= 10^5
  29. queries[i].length == 2
  30. 0 <= indexi <= n - 1
  31. 1 <=  colori <= 10^5
  32.  
  33. ---------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35. class Solution {
  36. public:
  37.     vector<int> colorTheArray(int n, vector<vector<int>>& queries) {
  38.         vector<int> res;
  39.         vector<int> nums(n,0);
  40.         int running=0;
  41.         for(auto q: queries){
  42.             int index=q[0];
  43.             int color=q[1];
  44.             if(index>0 && nums[index-1]==nums[index] && nums[index]!=0){
  45.                 running--;
  46.             }
  47.             if(index<n-1 && nums[index+1]==nums[index] && nums[index]!=0){
  48.                 running--;
  49.             }
  50.             nums[index]=color;
  51.             if(index>0 && nums[index-1]==nums[index]){
  52.                 running++;
  53.             }
  54.             if(index<n-1 && nums[index+1]==nums[index]){
  55.                 running++;
  56.             }
  57.             res.push_back(running);
  58.         }
  59.         return res;
  60.     }
  61. };
Advertisement
Add Comment
Please, Sign In to add comment