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 ...
随机推荐
- 利用十字链表存储树结构(便于同时求出某一点的入度与出度)------C语言实现
#include <stdio.h> #include<conio.h> #include<stdlib.h> /* 利用十字链表存储有向图,可用于同时查找某个顶点 ...
- 小程序在wxml页面格式化类似的2019-02-16T10:54:47.831000时间
其实新建小程序的时候,会有一个util.js文件,这个文件里已经有时间格式化的方法了,可是它却不能再wxml页面调用, 不过wxml页面是支持引入.wxs文件的,我们重新写一个这样子的工具文件就解决了 ...
- 函数式编程/lambda表达式入门
函数式编程/lambda表达式入门 本篇主要讲解 lambda表达式的入门,涉及为什么使用函数式编程,以及jdk8提供的函数式接口 和 接口的默认方法 等等 1.什么是命令式编程 命令式编程就是我们去 ...
- 从零开始学习MySQL全文索引
目录 一.为什么要用全文索引 二.什么是全文索引 三.如何创建全文索引 四.创建测试数据 五.查询-使用自然语言模式 六.查询-使用布尔模式(强大的语法) 语法 示例 七.查询-使用扩展模式 八.注意 ...
- 08.JS单词整理
以下为按照文章顺序简单整理的JS单词, 注意:是JS单词注释,部分与英文不符 01.JS语法规范.变量与常量 console——控制台 log——日志 var——变量 variable变量,变化 co ...
- SQL Server解惑——对象命名的唯一性小结
关于SQL Server数据库中的对象命名的唯一性问题.例如表.索引.约束等数据库对象,有时候DBA在做数据库维护时,经常要创建对象或重命名对象,此时就会遇到一个问题,对象命名的唯一性问题.虽然是一个 ...
- Ubuntu 1910安装Openshift 4.0单机版 (CRC)
Openshift默认可以在CentOS等RHEL系的发行版上安装. 本文转述一下如何在Ubuntu 1910上安装Openshift4.0单机版(CRC). 原文请参考: https://gith ...
- Java数据结构--双向链表的实现
#java学习经验总结------双向链表的实现 双向链表的建立与单链表类似,只是需要使用pre指针指向前一个结点,并且在删除添加时不仅仅考虑next package datastructure; p ...
- 【已解决】pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in position 110: invalid continuation byte
转载自勤奋的小青蛙本文链接地址: [已解决]pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in positi ...
- 【python数据挖掘】批量爬取站长之家的图片
概述: 站长之家的图片爬取 使用BeautifulSoup解析html 通过浏览器的形式来爬取,爬取成功后以二进制保存,保存的时候根据每一页按页存放每一页的图片 第一页:http://sc.china ...