LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意
给定一个正整数数组和K,数有多少个连续子数组满足:
数组中所有的元素的积小于K.
思路
依旧是双指针的思路
我们首先固定右指针r.
现在子数组的最右边的元素是nums[r].
我们让这个子数组尽可能的长,尽可能的往左边拓展,假设最左边的元素的前一个元素是l.
即子数组(l,r].
显然对于以nums[r]结尾的满足题意的数组个数为r−lr-lr−l
对于
(l1,r1],  (l2,r2](r1<r2)(l_1,r_1],\;(l_2,r_2] \quad (r_1<r_2)(l1,r1],(l2,r2](r1<r2)
由于数组元素都是正数,易证
l1<l2l_1 < l_2l1<l2
举个例子(其实证明也是这个反正的思路):
加入(2,5]和(1,6]同时存在。
- (2,5] 第5个数结尾,左边最多到第2个。 ①
- (1,6] 第6个数结尾,左边最多到第1个。
那么换句话第一个数到第五个数的乘积肯定不超过k,那和①这一行的(2,5]矛盾了。
因此l关于r是非严格单调递增(换句话说不下降)的。
明白了这一点就可以具体的设计了。
由r快速转移到r+1的状态
假设对于r我们已经求得了(l,r]。
那么对于r+1的问题,我们如何快速转移求解呢?
- 更新product,乘以新增的nums[r]
- 基于单调性,我们只需从上一步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 题解 双指针+单调性的更多相关文章
- LeetCode Subarray Product Less Than K
原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- Subarray Product Less Than K LT713
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- [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 ...
- 713. Subarray Product Less Than K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- leetcode713 Subarray Product Less Than K
""" Your are given an array of positive integers nums. Count and print the number of ...
- Subarray Product Less Than K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [leetcode] 713. Subarray Product Less Than K
题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...
随机推荐
- Unity5.5.6 升级到 2018.4.1 打包出现的问题 : Gradle version 2.10 is required. Current version is 5.1.1
起因:最近要在googleplay上架新游戏,而谷歌要求新上架的应用要支持64位,鉴于老版本的unity不支持打包64位apk,所以决定升级unity版本到2018.4.1, 但打包过程中出现了几个问 ...
- Rust学习--变量
0x0 每种编程语言都有变量的概念,我们可以把变量理解为最简单的存储方式,它是编码过程中是必不可少的. Rust的变量很有特色.变量不可变的特性让人想起了Erlang.以及后面的模式匹配,我觉得作者应 ...
- Day1前端学习之路——概述
终于下定决心要好好学习前端知识了,以后会把学习过程中的一些随笔记录在这里.HTML.CSS.JavaScript这三大前端语言在大三的时候就有所接触,但是学习的不够深入,这一次希望能够坚持下去. 学习 ...
- Python Flask 开发学习笔记
Flask学习 安装pipenv虚拟环境 pip Install pipenv 运行pipenv pipenv --version 进入虚拟容器 pipenv install 安装flask pipe ...
- MacBook通过SSH远程访问Parallel中的Ubuntu简明教程
作为一个前端,后端也需要了解,最终选择PHP入手学习,本来想选择Python,思前想后还是PHP作为Web开发比较合适,环境最终选择Ubuntu开发,由于是第一次,遇到不少坑,经过不懈的努力不断Goo ...
- OpenResty 在马蜂窝广告监测中的应用
马蜂窝技术原创内容,更多干货请订阅公众号:mfwtech 广告是互联网变现的重要手段之一. 以马蜂窝旅游 App 为例,当用户打开我们的应用时,有可能会在首屏或是信息流.商品列表中看到推送的广告.如果 ...
- Android-ServiceManager
ServiceManager在init进程启动之后启动,用来管理系统中的service,那么首先理解一下在init进程启动之后启动这句话类: 一般开机过程分为三个阶段: OS级别,由bootloade ...
- Python学习零基础<入门必学>
1. 注释注释 是任何存在于 # 号右侧的文字,其主要用作写给程序读者看的笔记. 2. 字面常量一个字面常量(Literal Constants)的例子是诸如 5.1.23 这样的数字,或者是如 这是 ...
- Git操作 :从一个分支cherry-pick多个commit到其他分支
在branch1开发,进行多个提交,这时切换到branch2,想把之前branch1分支提交的commit都[复制]过来,怎么办? 首先切换到branch1分支,然后查看提交历史记录,也可以用sour ...
- java 上传文件到七牛云中
import com.alibaba.fastjson.JSONObject;import com.qiniu.common.QiniuException;import com.qiniu.commo ...