leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2799.cpp (581B)


      1 class Solution {
      2   public:
      3     int countCompleteSubarrays(const vector<int> &nums) {
      4         static int count[10001];
      5         memset(count, 0x00, sizeof(count));
      6 
      7         int uniq = 0, res = 0, cnt = 0;
      8         ;
      9         for (const int n : nums)
     10             if (!count[n]) count[n] = 1, uniq++;
     11         for (int i = 0, left = 0; i < nums.size(); i++) {
     12             if (count[nums[i]]++ == 1) cnt++;
     13             while (cnt == uniq) {
     14                 res += nums.size() - i;
     15                 if (--count[nums[left++]] == 1) cnt--;
     16             }
     17         }
     18         return res;
     19     }
     20 };