题意

给定一个正整数数组和K,数有多少个连续子数组满足:

数组中所有的元素的积小于K.

思路

依旧是双指针的思路

我们首先固定右指针r.

现在子数组的最右边的元素是nums[r].

我们让这个子数组尽可能的长,尽可能的往左边拓展,假设最左边的元素的前一个元素是l.

即子数组(l,r].

显然对于以nums[r]结尾的满足题意的数组个数为r−lr-lr−l

对于

(l1,r1],&ThickSpace;(l2,r2](r1&lt;r2)(l_1,r_1],\;(l_2,r_2] \quad (r_1&lt;r_2)(l1​,r1​],(l2​,r2​](r1​<r2​)

由于数组元素都是正数,易证

l1&lt;l2l_1 &lt; l_2l1​<l2​

举个例子(其实证明也是这个反正的思路):

加入(2,5]和(1,6]同时存在。

  1. (2,5] 第5个数结尾,左边最多到第2个。 ①
  2. (1,6] 第6个数结尾,左边最多到第1个。

    那么换句话第一个数到第五个数的乘积肯定不超过k,那和①这一行的(2,5]矛盾了。

    因此l关于r是非严格单调递增(换句话说不下降)的。

    明白了这一点就可以具体的设计了。

由r快速转移到r+1的状态

假设对于r我们已经求得了(l,r]。

那么对于r+1的问题,我们如何快速转移求解呢?

  1. 更新product,乘以新增的nums[r]
  2. 基于单调性,我们只需从上一步r的l开始验证,如果不符合则l后移

复杂度

T(N)=O(N),M(N)=O(1)T(N)=O(N),M(N)=O(1)T(N)=O(N),M(N)=O(1)

结果

(14ms, 80.5%, 52.7MB, 100.00%)

Source Code

class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int len = nums.length;
int product = 1;
int count = 0;
// (l,r]
int l = -1, r = 0;
while (r < len) {
product *= nums[r];
while (l < r && product >= k)
product /= nums[++l];
count += r - l;
++r;
}
return count;
}
}

参考链接

有了思路之后,还参考了GrandYang的博客

LeetCode Subarray Product Less Than K 题解 双指针+单调性的更多相关文章

  1. LeetCode Subarray Product Less Than K

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

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

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

  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】713. Subarray Product Less Than K 解题报告(Python)

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

  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. 713. Subarray Product Less Than K

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

  7. leetcode713 Subarray Product Less Than K

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

  8. Subarray Product Less Than K

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

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

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

随机推荐

  1. 兄弟连 企业shell笔试题 16-31

    企业实践题16:企业案例:写网络服务独立进程模式下rsync的系统启动脚本 例如:/etc/init.d/rsyncd{start|stop|restart} .要求:1.要使用系统函数库技巧.2.要 ...

  2. 制作openstack的windows server 2012r2镜像

    1. 基础环境安装 yum groupinstall Virtualization "Virtualization Client" yum install libvirt 2. 启 ...

  3. 增加yum源方式 安装升级 Mysql

             MySQL官方新提供了一种安装MySQL的方法--使用YUM源安装MySQL 1.MySQL官方网站下载MySQL的YUM源,  https://dev.mysql.com/down ...

  4. Apache的那些事-查找配置文件

    在CentOS 6.5 里Apache的 安装后出现两个httpd.conf配置文件,一个在          /etc/httpd/conf/httpd.conf             这个事li ...

  5. IIS6的文件解析漏洞

    IIS6的默认配置漏洞会把cer.cdx.asa作为asp代码来解析 后缀解析漏洞 /test.asp;.jpg 或者/test.asp:.jpg(此处需抓包修改文件名) IIS6.0 都会把此类后缀 ...

  6. Java线程池中线程的生命周期

    设:我们有一个coreSize=10,maxSize=20,keepAliveTime=60s,queue=40 1.池初始化时里面没有任何线程. 2.当有一个任务提交到池就创建第一个线程. 3.若继 ...

  7. 在ServletFilter层返回多字段提示给前端

    0.背景:在由于不想在小项目中引入SpringSecurity这种重量级安全框架,我自定义了一个LoginFilter来处理认证+授权逻辑.对于认证或授权失败的情况,最初是在filter中抛出异常,并 ...

  8. Android中获取目标布局文件中的组件

    方法如下: LayoutInflater flater= LayoutInflater.from(getContext()); //R.layout.title处填写目标布局 final View v ...

  9. yarn-site.xml 配置介绍

    yarn-site.xml 配置介绍 yarn.scheduler.minimum-allocation-mb yarn.scheduler.maximum-allocation-mb 说明:单个容器 ...

  10. 基于云开发开发 Web 应用(二):界面 UI 开发

    工作量分析 在我们进行这部分开发的时候,接下来我们需要进行相应的功能安排和分类. 简单看来,我需要开发 3 个页面: 首页:首页负责用户默认访问. 列表页:列表页面则是在搜索过程中,如果有多个结果,则 ...