leetcode

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

1248.cpp (430B)


      1 class Solution {
      2     int atMost(const vector<int> &nums, int k) {
      3         int res = 0, j = 0;
      4         for (int i = 0; i < nums.size(); i++) {
      5             if (nums[i] % 2) k--;
      6             while (k < 0)
      7                 if (nums[j++] % 2) k++;
      8             res += i - j + 1;
      9         }
     10         return res;
     11     }
     12 
     13   public:
     14     int numberOfSubarrays(const vector<int> &nums, int k) { return atMost(nums, k) - atMost(nums, k - 1); }
     15 };