返回介绍

solution / 0700-0799 / 0713.Subarray Product Less Than K / README_EN

发布于 2024-06-17 01:03:35 字数 4023 浏览 0 评论 0 收藏 0

713. Subarray Product Less Than K

中文文档

Description

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 * 104
  • 1 <= nums[i] <= 1000
  • 0 <= k <= 106

Solutions

Solution 1

class Solution:
  def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
    ans, s, j = 0, 1, 0
    for i, v in enumerate(nums):
      s *= v
      while j <= i and s >= k:
        s //= nums[j]
        j += 1
      ans += i - j + 1
    return ans
class Solution {
  public int numSubarrayProductLessThanK(int[] nums, int k) {
    int ans = 0;
    for (int i = 0, j = 0, s = 1; i < nums.length; ++i) {
      s *= nums[i];
      while (j <= i && s >= k) {
        s /= nums[j++];
      }
      ans += i - j + 1;
    }
    return ans;
  }
}
class Solution {
public:
  int numSubarrayProductLessThanK(vector<int>& nums, int k) {
    int ans = 0;
    for (int i = 0, j = 0, s = 1; i < nums.size(); ++i) {
      s *= nums[i];
      while (j <= i && s >= k) s /= nums[j++];
      ans += i - j + 1;
    }
    return ans;
  }
};
func numSubarrayProductLessThanK(nums []int, k int) int {
  ans := 0
  for i, j, s := 0, 0, 1; i < len(nums); i++ {
    s *= nums[i]
    for ; j <= i && s >= k; j++ {
      s /= nums[j]
    }
    ans += i - j + 1
  }
  return ans
}
function numSubarrayProductLessThanK(nums: number[], k: number): number {
  let ans = 0;
  for (let i = 0, j = 0, s = 1; i < nums.length; ++i) {
    s *= nums[i];
    while (j <= i && s >= k) {
      s /= nums[j++];
    }
    ans += i - j + 1;
  }
  return ans;
}
impl Solution {
  pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
    if k <= 1 {
      return 0;
    }

    let mut res = 0;
    let mut product = 1;
    let mut i = 0;
    nums.iter()
      .enumerate()
      .for_each(|(j, v)| {
        product *= v;
        while product >= k {
          product /= nums[i];
          i += 1;
        }
        res += j - i + 1;
      });
    res as i32
  }
}
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var numSubarrayProductLessThanK = function (nums, k) {
  const n = nums.length;
  let ans = 0;
  let s = 1;
  for (let i = 0, j = 0; i < n; ++i) {
    s *= nums[i];
    while (j <= i && s >= k) {
      s = Math.floor(s / nums[j++]);
    }
    ans += i - j + 1;
  }
  return ans;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文