Your are given an array of positive integers nums.

Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is 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.

Note:

  • 0 < nums.length <= 50000.
  • 0 < nums[i] < 1000.
  • 0 <= k < 10^6.

Approach #1. Math. [Java]

class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int pro = 1;
int cnt = 0; for (int i = 0, j = 0; j < nums.length; ++j) {
pro *= nums[j];
while (i <= j && pro >= k) {
pro /= nums[i++];
}
cnt += j - i + 1;
} return cnt;
}
}

  

Analysis:

The idea is always keep an max-product-window less than K;

Every time shift window by adding a new number on the right(j), if the product is greater than K, then try to reduce numbers on the left(i), untill the subarray product fit less than K again, (subarray could be empty);

Each step introduces x new  subarrays, where x is the size of the current window (j - i + 1);

Example:

for window (5, 2), when 6 is ntroduced, it add 3 new subarray: (5, (2, (6)))

Reference:

https://leetcode.com/problems/subarray-product-less-than-k/discuss/108861/JavaC%2B%2B-Clean-Code-with-Explanation

713. Subarray Product Less Than K的更多相关文章

  1. 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...

  2. [leetcode] 713. Subarray Product Less Than K

    题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...

  3. Subarray Product Less Than K LT713

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  4. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  5. [Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  6. LeetCode Subarray Product Less Than K

    原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...

  7. Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  8. leetcode713 Subarray Product Less Than K

    """ Your are given an array of positive integers nums. Count and print the number of ...

  9. LeetCode Subarray Product Less Than K 题解 双指针+单调性

    题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...

随机推荐

  1. snowflake自增ID算法 (PHP版)

    /** * SnowFlake ID Generator * Based on Twitter Snowflake to generate unique ID across multiple * da ...

  2. 清除所有Cookie

    代码 /// <summary> /// 清除所有Cookie /// </summary> public static void RemoveAll() { System.W ...

  3. [远程] windows 2008 server设置了共享文件夹,并且共享给了everyone,但是还是无法访问,怎么解决呢?

    还需要设置另外一个地方,将用户加到MSAppAccess这个组里去

  4. 4.spring对象的创建(静态工厂 实例工厂 泛型,嵌套类型)

    1.原料类 namespace CreateObjects{    public class GenericClass<T>    { }} PersonDao 类 包含嵌套类型 name ...

  5. how to enable the Accessibility in the app

    第一部分 先要装一个accchecker,全称是 UI Accessibility Checker .下载地址: http://acccheck.codeplex.com/ 装了之后 用这个工具可以 ...

  6. 2018.08.04 bzoj3261: 最大异或和(trie)

    传送门 简单可持久化01trie树. 实际上这东西跟可持久化线段树貌似是一个东西啊. 要维护题目给出的信息,就需要维护前缀异或和并且把它们插入一棵01trie树,然后利用贪心的思想在上面递归就行了,因 ...

  7. 数塔问题-hdu-2084(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 思路:要求从顶到底的最大值,可以反过来考虑,从底部向上. 只有下面一行的最大值确定,这一行的最大 ...

  8. 解决Docker时区与主机时区不一致的问题

    在Dockerfile里面增加以下红色的部分 FROM hub.chinacloud.com/common/jdk:8MAINTAINER xxx@chinacloud.com.cn RUN mkdi ...

  9. SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片

    SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片   在上一篇文章中,我们介绍了< SpringMVC上传图片的常规上传方法 >.本文接着第一 ...

  10. 自定义方法实现strcpy,strlen, strcat, strcmp函数,了解及实现原理

    位置计算字符串长度 //strlen()函数,当遇到'\0'时,计算结束,'\0'不计入长度之内 //字符串的拷贝        //strcpy(字符串1,字符串2);        //把字符串2 ...