add: 220505-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-05 22:31:15 +08:00
parent 2b767f9fb3
commit d5b8e06333
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 37 additions and 1 deletions

36
cpp/2205/220505-CN.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <vector>
/**
* 713. Subarray Product Less Than K
* Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.
*/
class Solution {
public:
static int numSubarrayProductLessThanK(const std::vector<int>& nums, int k) {
long long t = nums[0];
int ans = (nums[0] < k) ? 1 : 0;
const int n = nums.size();
// Range [i, j) --> product = t
for (int i = 0, j = 1; j < n; ++j) {
// t now: product of range [i, j)
while (t * nums[j] >= k && i < j) {
// Advance i
// After this, i will be at most j.
t /= nums[i++];
}
// t now: product of range [i, j]
t *= nums[j];
// [i, j] is now sufficient to be an answer, if nums[j] < k
if (j - i >= 0 && nums[j] < k)
// Non-contiguous array: += 1 << (j - i);
ans += j - i + 1;
// j <- 1 + j, at the start of next loop, t will again be product of range [i, j).
}
return ans;
}
};
int main() {
std::printf("%d\n", Solution::numSubarrayProductLessThanK({10,5,2,6},100));
}

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220504.cpp) ADD_EXECUTABLE(2204 220505-CN.cpp)