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:

  1. Input: nums = [10, 5, 2, 6], k = 100
  2. Output: 8
  3. Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
  4. 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]

  1. class Solution {
  2. public int numSubarrayProductLessThanK(int[] nums, int k) {
  3. int pro = 1;
  4. int cnt = 0;
  5.  
  6. for (int i = 0, j = 0; j < nums.length; ++j) {
  7. pro *= nums[j];
  8. while (i <= j && pro >= k) {
  9. pro /= nums[i++];
  10. }
  11. cnt += j - i + 1;
  12. }
  13.  
  14. return cnt;
  15. }
  16. }

  

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. PAT 1070 结绳(25)(代码)

    1070 结绳(25 分) 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每 ...

  2. 查询yum包安装路径

    rpm -ql php71-php yum install json yum install libcurl

  3. npm 如何设置镜像站为淘宝网

    转载 2015年06月24日 17:12:12 10542 淘宝镜像:http://npm.taobao.org/ 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候 ...

  4. oracle 存储过程应用

    1.查看 SELECT * FROM all_source WHERE type='PROCEDURE' and name=upper('liuyi_prcd'); 2.删除 DROP PROCEDU ...

  5. iOS沙盒机制介绍

    一.沙盒机制 沙盒的概念:沙盒是每一个iOS应用程序都会自动创建的一个文件系统目录(文件夹),而且沙盒还具有独立.封闭.安全的特点. 沙盒机制 iOS中的沙盒不仅仅是一个文件目录,TA其实更是一种安全 ...

  6. Windows Server 2012如何实现双网卡绑定

    在windows server 2012 之前我们在服务器上如果要实现双网卡绑定则需要向服务器厂家所要相应的软件,但是现在强大的windows server 2012的到来使我们省去了所有的麻烦,因为 ...

  7. Django入门与实践-第25章:Markdown 支持(完结)

    http://127.0.0.1:8000/boards/1/topics/102/reply/ 让我们在文本区域添加 Markdown 支持来改善用户体验. 你会看到要实现这个功能非常简单. 首先, ...

  8. flask_web表单

    一.配置 1.为了能够处理 web 表单,我们将使用 Flask-WTF,该扩展封装了 WTFForms 并且恰当地集成进 Flask 中.许多 Flask 扩展需要大量的配置,因此我们将要在 mic ...

  9. linux中nfs启动报rpcbind.socket failed to listen on sockets: Address family not supported by protocol

    1.systemctl start rpcbind.service 报错: [root@autodeploy ~]# journalctl -xe -- Support: http://lists.f ...

  10. Mouse Touch Stylus

    Mouse操作: preview mouse down, StylusDevice:null mouse down,StylusDevice:null preview mouse up, Stylus ...