[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^41 <= nums[i] <= 10000 <= 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]. 我们让这个子数组尽 ...
随机推荐
- KingbaseES R3 读写分离集群在线扩容案例
案例说明: 1. 通过sys_basebackup创建新备库. 2. 将备库加入到Cluster nodes管理,可以用kingbase_monitor.sh一键启停. 3. 主备复制切换测试. 此次 ...
- 【android逆向】 ARM for 逆向
C源码 #include <stdio.h> int nums[5] = {1, 2, 3, 4, 5}; int for1(int n){ //普通for循环 int i = 0; in ...
- Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):2、PGP下载安装与密钥生成发布
文章目录: Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):1.JIRA账号注册 Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):2.PGP ...
- Bypass Windows Defender Dump Lsass(手法拙劣)
0x00.前言 Windows Defender是一款内置在Windows操作系统的杀毒软件程序,本文旨在记录实战环境中,服务器存在Windows Defender情况下转储凭证的渗透手法,技术简单粗 ...
- LIMIT和OFFSET分页性能差!今天来介绍如何高性能分页
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. GreatSQL是MySQL的国产分支版本,使用上与MySQL一致. 前言 之前的大多数人分页采用的都是这样: SELEC ...
- 关于指针初始化为NULL的一些问题
关于指针初始化问题,先看以下代码: #include <stdio.h>typedef struct{ char data[128]; int top;} Stack;voi ...
- Linkerd、Consul、Istio、Kuma、Traefik、AWS App服务网格全方位对比
- 通过 Docker 部署 Mysql 8.0 主从模式
文章转载自:http://www.mydlq.club/article/106/ 系统环境: Mysql 版本:8.0.23 Docker 版本:19.03.13 一.为什么需要 Mysql 主从复制 ...
- 14. 第十三篇 二进制安装kube-proxy
文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247484231&idx=1&sn=9e722bee ...
- Elasticsearch不支持事务有什么好的弥补方案
1.问题 源自星球同学的提问:es如何与hive或mysql结合使用?es不支持事务有什么好的弥补方案吗? 2.事务的核心概念 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下ACID四个 ...