leetcode

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

2444.cpp (587B)


      1 class Solution {
      2   public:
      3     long long countSubarrays(vector<int> &nums, int minK, int maxK) {
      4         int n = nums.size(), leftBound = -1, lastMin = -1, lastMax = -1;
      5         long long count = 0;
      6 
      7         for (int i = 0; i < n; i++) {
      8             if (nums[i] < minK || nums[i] > maxK) {
      9                 leftBound = i, lastMin = -1, lastMax = -1;
     10                 continue;
     11             }
     12             if (nums[i] == minK) lastMin = i;
     13             if (nums[i] == maxK) lastMax = i;
     14             count += max(0, min(lastMin, lastMax) - leftBound);
     15         }
     16 
     17         return count;
     18     }
     19 };