aboutsummaryrefslogtreecommitdiff
path: root/subarr_product_less_k.cpp
blob: 64cb1b4b42610a98c7ffd1dfc727f926b9dd8b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "leetcode.h"

class Solution {
public:
  int numSubarrayProductLessThanK(vector<int> &nums, int k) {
    if (k == 0)
      return 0;
    int count = 0, prod = 1;
    for (int low = 0, high = 0; high < nums.size(); high++) {
      prod *= nums[high];
      while (low <= high && prod >= k)
        prod /= nums[low++];
      count += (high - low + 1);
    }
    return count;
  }
};

int main() {
  Solution obj;
  vector<int> nums = {10, 5, 2, 6};
  cout << obj.numSubarrayProductLessThanK(nums, 100);
}