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 ...
随机推荐
- Linux文本界面字体颜色修改
环境 基于centos 6.5 在文本界面 系统目录的字体颜色是 黑底蓝字 严重看不清楚,对此作出修改 使用 vi 编辑 进入 /etc/DIR_COLORS 找到“DIR 01;34 # ...
- python随用随学20200220-异步IO
啥是异步IO 众所周知,CPU速度太快,磁盘,网络等IO跟不上. 而程序一旦遇到IO的时候,就需要等IO完成才能进行才能进行下一步的操作. 严重拖累了程序速度. 因为一个IO操作就阻塞了当前线程,导致 ...
- Python json 序列号字典 文本的存储和读取
rootDir='./resources/v1/'# 根目录 # 按钮测试图片 btnTestPicUrl = { 'armyAttack' : rootDir+'testPic/gj2.jpg', ...
- App 抓包提示网络异常怎么破?
背景 当你测试App的时候,想要通过Fiddler/Charles等工具抓包看下https请求的数据情况,发现大部分的App都提示网络异常/无数据等等信息.以"贝壳找房"为例: F ...
- 【python人脸识别】使用opencv识别图片中的人脸
概述: OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库 为什么有OpenCV? 计算机视觉市场巨大而且持续增长,且这方面没有标准API,如今的计算机视觉软件大概有以下三种: 1.研究 ...
- Eclipse部署项目,常见几个问题解决方案
一.java compiler level does not match the version of the inst 解决方案:(一般出现在拷贝项目) 第一步:在Eclipse环境中,鼠标右键选择 ...
- STL专题
一.algorithm 1.sort 问题1:给你n个整数,请按从大到小的顺序输出其中前m大的数. Input:每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二 ...
- Linux下的python3,virtualenv,Mysql、nginx、redis等常用服务安装配置
Linux下的python3,virtualenv,Mysql.nginx.redis等常用服务安装配置 学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环 ...
- opencv —— 同时识别三种颜色
要点: 1.识别一种颜色 minH = ; //色相 maxH = ; minS = ; //饱和度 maxS = ; minV = ; // inRange(原图像, 最小值的范围, 最大值的范围, ...
- adworld easy_RSA | RSA算法
题目描述: 解答出来了上一个题目的你现在可是春风得意,你们走向了下一个题目所处的地方 你一看这个题目傻眼了,这明明是一个数学题啊!!!可是你的数学并不好.扭头看向小鱼,小鱼哈哈一笑 ,让你在学校里面不 ...