diff --git a/cpp/2205/220505-CN.cpp b/cpp/2205/220505-CN.cpp new file mode 100644 index 0000000..c05bd6d --- /dev/null +++ b/cpp/2205/220505-CN.cpp @@ -0,0 +1,36 @@ +#include + +/** + * 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& 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)); +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 913d619..5a7fe14 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220504.cpp) +ADD_EXECUTABLE(2204 220505-CN.cpp)