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 ...
随机推荐
- ASP.NET MVC通用权限管理系统(响应布局)源码更新介绍
一.asp.net mvc 通用权限管理系统(响应布局)源码主要以下特点: AngelRM(Asp.net MVC)是基于asp.net(C#)MVC+前端bootstrap+ztree+lodash ...
- MS14-068提权和impacket工具包提权
ms14-068提权 工具利用 a)拿下边界机win7,并已经有win7上任意一个账号的密码 -u 用户名@域 -p 用户密码 -s 用户sid -d 域控 ms14-068.exe -u test3 ...
- Leetcode:235. 二叉搜索树的最近公共祖先
Leetcode:235. 二叉搜索树的最近公共祖先 Leetcode:235. 二叉搜索树的最近公共祖先 Talk is cheap . Show me the code . /** * Defin ...
- k8s强制删除pod
有时候pod一直在Terminating kubectl delete pod xxx --force --grace-period=
- 手动配置IP地址
更改适配器属性 -> 选择一个网络,单击右键 -> 属性 -> Internet 协议版本 4 (TCP/IPv4)-> 属性 进入如下界面,开始配置: IP地址: IP地址用 ...
- mybatis 配置--->确认jar包是否正确
mybatis 配置之前,首先要确保服务器jar包是否成功 配置jar包如下添加mybaties-3.5.2. jar, maven 的 pom.xml 配置如下,查看配置是否成功见 如上分类 Mav ...
- xshell/secureCRT连接Linux及其常用命令
一.xshell:在Windows界面下用来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的 下载安装后连接步骤: 二.secureCRT:在Windows下登录UNIX或Linux服务 ...
- jquery ajax简单书写
占时无法显示该内容,请稍后再试 $.ajax({ url:"http://v.juhe.cn/weather/index", data:{cityname:"苏州&quo ...
- STL入门学习中碰到的一些函数
2020.02.10 fill #include<algorithm> vector<int> v{ 1, 2, 3, 3 }; fill(v.begin(), v.end() ...
- c++多线程编程互斥锁初步
上一次讲述了多线程编程,但是由于线程是共享内存空间和资源的,这就导致:在使用多线程的时候,对于共享资源的控制要做的很好.先上程序: #include <iostream> #include ...