forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
53 lines (42 loc) · 1.38 KB
/
main2.cpp
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
51
52
53
/// Source : https://leetcode.com/problems/subarray-product-less-than-k/description/
/// Author : liuyubobobo
/// Time : 2017-10-32
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
/// log the numbers and binary search
///
/// Time Complexity: O(n*logn), n = len(nums)
/// Space Complexity: O(n)
class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
vector<double> logsums;
logsums.push_back(0.0);
for(int num: nums)
logsums.push_back(log((double)num));
double logk = log((double)k);
for(int i = 1 ; i <= logsums.size() ; i ++)
logsums[i] += logsums[i-1];
int res = 0;
for(int i = 1 ; i < logsums.size() ; i ++){
vector<double>::iterator iter =
lower_bound(logsums.begin() + i, logsums.end(),
logsums[i-1] + logk);
res += (iter - logsums.begin()) - i;
}
return res;
}
};
int main() {
int nums1[] = {10, 5, 2, 6};
int k1 = 100;
vector<int> vec1(nums1, nums1 + sizeof(nums1)/sizeof(int));
cout << Solution().numSubarrayProductLessThanK(vec1, k1) << endl;
int nums2[] = {1, 2, 3};
int k2 = 0;
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
cout << Solution().numSubarrayProductLessThanK(vec2, k2) << endl;
return 0;
}