Advertisement
SalmaYasser

Untitled

Jan 13th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isPossible(vector<int>& nums) {
  4.  
  5. unordered_map <int , int > freq , extend;
  6.  
  7. for (int i = 0 ; i < nums.size(); i++)
  8. {
  9. freq[nums[i]] ++;
  10. }
  11.  
  12. for (int i = 0 ; i < nums.size(); i++)
  13. {
  14. int cur = nums[i];
  15. if (freq[cur] == 0)
  16. continue;
  17.  
  18. if (extend[cur] > 0)
  19. {
  20. freq[cur]--;
  21. extend[cur]--;
  22. extend[cur + 1]++;
  23. }
  24. else if (freq[cur + 1] > 0 && freq[cur + 2] > 0)
  25. {
  26. freq[cur]--;
  27. freq[cur + 1]--;
  28. freq[cur + 2]--;
  29. extend[cur + 3]++;
  30. }
  31. else
  32. return false;
  33. }
  34. return true;
  35. }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement