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 ...
随机推荐
- KVM性能优化之CPU优化
前言 任何平台根据场景的不同,都有相应的优化.不一样的硬件环境.网络环境,同样的一个平台,它跑出的效果也肯定不一样.就好比一辆法拉利,在高速公路里跑跟乡村街道跑,速度和激情肯定不同... 所以,我们做 ...
- Python学习小记(3)---scope&namespace
首先,函数里面是可以访问外部变量的 #scope.py def scope_test(): spam = 'scope_test spam' def inner_scope_test(): spam ...
- IntelliJ 如何找到项目中 Deprecated 的方法
在一个项目中,如果我们标记了某些元素为 Deprecated 的话,如何让我们能够快速找到? 简单来说,你可以对项目进行 Code Inspection. 选择 Analyze > Inspec ...
- linux之ls目录处理命令
目录处理命令:ls 解释 命令名称:ls 命令英文原意:list 命令所在路径:/bin/ls 执行权限:所有用户 功能描述:显示目录文件 语法 ls 选项[-ald] [文件或目录] -a 显示所有 ...
- C# protobuf自动更新cs文件
网上的教程大都是手动通过protoc编译, 比较难用 给当前工程添加"Google.Protobuf"和"Grpc.Tools"的引用(通过nuget), 然后 ...
- SAP 序列号与库存关联起来?
SAP 序列号与库存关联起来? SAP系统标准功能可以实现序列号管理.其系统配置也不复杂,但是不少企业却使用不起来.笔者参与的诸多项目里,只有现在所在的项目里有启用序列号管理.基于项目客户所在行业,以 ...
- 常见Linux命令学习
Linux命令学习 命令分类: 文件处理命令 权限管理命令 文件搜索命令 帮助命令 用户管理命令 压缩解压命令 网络命令 关机重启命令 1.文件处理命令 命令格式:命令 [-选项] [参数] 例:ls ...
- 安装NodeJs和NPM到Ubuntu(APT)
运行环境 系统版本:Ubuntu 16.04.2 LTS 软件版本:node-v10.16.3.npm-6.9.0 硬件要求:无 安装过程 1.安装NPM和NodeJs root@localhost: ...
- Rx基础
>>返回<C# 并发编程> 1. 转换.NET事件 1.1. 进度通知 1.2. 定时器示例 1.3. 错误传递 2. 发通知给上下文 3. 用窗口和缓冲对事件分组 4. 用限 ...
- 今天带来compass的使用方式
一.为什么我们要使用compass呢 Experience cleaner markup without presentational classes. It’s chock full of the ...