| comments | true | ||||
|---|---|---|---|---|---|
| difficulty | Medium | ||||
| edit_url | https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README_EN.md | ||||
| tags |
|
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.
Example 1:
Input: nums = [10,5,2,6], k = 100 Output: 8 Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6] Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
Example 2:
Input: nums = [1,2,3], k = 0 Output: 0
Constraints:
1 <= nums.length <= 3 * 1041 <= nums[i] <= 10000 <= k <= 106
We can use two pointers to maintain a sliding window, where the product of all elements in the window is less than
Define two pointers
Each time, we move
The time complexity is
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
ans = l = 0
p = 1
for r, x in enumerate(nums):
p *= x
while l <= r and p >= k:
p //= nums[l]
l += 1
ans += r - l + 1
return ansclass Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int ans = 0, l = 0;
int p = 1;
for (int r = 0; r < nums.length; ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
}
}class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
int ans = 0, l = 0;
int p = 1;
for (int r = 0; r < nums.size(); ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
}
};func numSubarrayProductLessThanK(nums []int, k int) (ans int) {
l, p := 0, 1
for r, x := range nums {
p *= x
for l <= r && p >= k {
p /= nums[l]
l++
}
ans += r - l + 1
}
return
}function numSubarrayProductLessThanK(nums: number[], k: number): number {
const n = nums.length;
let [ans, l, p] = [0, 0, 1];
for (let r = 0; r < n; ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
}impl Solution {
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
let mut ans = 0;
let mut l = 0;
let mut p = 1;
for (r, &x) in nums.iter().enumerate() {
p *= x;
while l <= r && p >= k {
p /= nums[l];
l += 1;
}
ans += (r - l + 1) as i32;
}
ans
}
}/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var numSubarrayProductLessThanK = function (nums, k) {
const n = nums.length;
let [ans, l, p] = [0, 0, 1];
for (let r = 0; r < n; ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
};class Solution {
fun numSubarrayProductLessThanK(nums: IntArray, k: Int): Int {
var ans = 0
var l = 0
var p = 1
for (r in nums.indices) {
p *= nums[r]
while (l <= r && p >= k) {
p /= nums[l]
l++
}
ans += r - l + 1
}
return ans
}
}public class Solution {
public int NumSubarrayProductLessThanK(int[] nums, int k) {
int ans = 0, l = 0;
int p = 1;
for (int r = 0; r < nums.Length; ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
}
}