[leetcode] 713. Subarray Product Less Than K
题目
Given an array of integers nums
and an integer k
, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k
.
Example 1:
Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
Example 2:
Input: nums = [1,2,3], k = 0
Output: 0
Constraints:
1 <= nums.length <= 3 * 10^4
1 <= nums[i] <= 1000
0 <= k <= 10^6
思路
双指针,左右指针框起来的数字需要保持积小于k,并在遍历nums的过程中不断地记录数量。
代码
python版本:
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
cnt, prod, l = 0, 1, 0
for r in range(len(nums)):
prod *= nums[r]
while prod >= k and l <= r:
prod /= nums[l]
l += 1
cnt += r-l+1
return cnt
[leetcode] 713. Subarray Product Less Than K的更多相关文章
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- 713. Subarray Product Less Than 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] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- LeetCode Subarray Product Less Than K
原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...
- [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 ...
- 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 ...
- LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...
随机推荐
- Docker 安装 MySQL、Redis
1 Docker 中安装 Redis 1.1 创建目录 在硬盘上创建 redis 的数据目录: mkdir -p /Users/yygnb/dockerMe/redis/data 为该目录添加权限: ...
- 专注效率提升「GitHub 热点速览 v.22.36」
本周最大的 GitHub 事件无疑是国内多家自媒体报道过的,GitHub 官方或将下架 GitHub Trending 页面.作为 GitHub Trending 长期用户,本周也是找到了实用且提升效 ...
- centos7部署Prometheus+Grafana
一.安装Prometheus Server 请从 Prometheus 官方下载 linux 版的二进制压缩包.注意在下载前要选择操作系统为 linux. 执行下面的命令把 prometheus se ...
- 1.云原生之Docker容器技术基础知识介绍
转载自:https://www.bilibili.com/read/cv15180540/?from=readlist
- Elastic:Elastic部署架构介绍
Elastic Stack是一套完整的从数据采集,解析,分析,丰富,到搜索,检索,数据程序等一套完整的软件栈.在具体的实践中,我们应该如何搭建我们的系统呢? 下图描述了常用的Elastic Stack ...
- Compass- 图形化界面客户端
到MongoDB官网下载MongoDB Compass, 地址: https://www.mongodb.com/download-center/v2/compass?initial=true 如果是 ...
- .NET 反向代理 YARP 跨域请求 CORS
使用过 nginx 的小伙伴应该都知道,这个中间件是可以设置跨域的,作为今天的主角,同样的 反向代理中间件的 YARP 毫无意外也支持了跨域请求设置. 有些小伙伴可能会问了,怎样才算是跨域呢? 在 H ...
- NSIS 检测默认浏览器
#检测默认浏览器 #编写:水晶石 #原理:用FindExecutable函数查找htm关联程序路径与名称,然后分析字串中包含的可执行文件名. !include "LogicLib.nsh&q ...
- centos7系统资源限制整理
概述 在linux系统使用过程中,默认的系统设置足够使用,但是对于一些高并发高性能的程序会有瓶颈存在,这些限制主要通过ulimit查看和修改. 环境 centos:CentOS release 7. ...
- Effective java 总结
用静态工厂方法代替构造器的最主要好处 1.不必每次都创建新的对象 Boolean.valueOf Long.valueOf 2.直接返回接口的子类型,对于外界来说并不需要关心实现细节,主要知道这个接口 ...