-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo713.cc
50 lines (45 loc) · 1.24 KB
/
No713.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Created by Xiaozhong on 2020/11/22.
* Copyright (c) 2020/11/22 Xiaozhong. All rights reserved.
*/
#include <iostream>
#include <vector>
using namespace std;
//class Solution {
//public:
// int numSubarrayProductLessThanK(vector<int> &nums, int k) {
// if (k <= 1) return 0;
// int product = 1, ans = 0, left = 0, right = 0;
// for (; right < nums.size(); ++right) {
// product *= nums[right];
// while (product >= k) product /= nums[left++];
// ans += (right - left + 1);
// }
// return ans;
// }
//};
namespace ez {
int accumulate(int left, int right) {
return (1 + right + 1) * (right - left + 1) / 2;
}
}
class Solution {
public:
int numSubarrayProductLessThanK(vector<int> &nums, int k) {
int n = static_cast<int>(nums.size());
int ans = 0;
int left = 0, right = 0, product = 1;
while (right < n) {
product *= nums[right];
while (product >= k) product /= nums[left++];
ans += (right - left + 1);
right++;
}
return ans;
}
};
int main() {
vector<int> nums = {10, 5, 2, 6};
Solution s;
cout << s.numSubarrayProductLessThanK(nums, 100) << endl;
}