当前位置:网站首页>【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
2022-08-03 19:24:00 【friedrichor】
剑指 Offer II 009. 乘积小于 K 的子数组
给定一个正整数数组 nums 和整数 k ,请找出该数组内乘积小于 k 的连续的子数组的个数。
示例 1:
输入: nums = [10,5,2,6], k = 100
输出: 8
解释: 8 个乘积小于 100 的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。
需要注意的是 [10,5,2] 并不是乘积小于100的子数组。
示例 2:
输入: nums = [1,2,3], k = 0
输出: 0
提示:
- 1 < = n u m s . l e n g t h < = 3 ∗ 1 0 4 1 <= nums.length <= 3 * 10^4 1<=nums.length<=3∗104
- 1 < = n u m s [ i ] < = 1000 1 <= nums[i] <= 1000 1<=nums[i]<=1000
- 0 < = k < = 1 0 6 0 <= k <= 10^6 0<=k<=106
题解
这题和 【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针) 比较相似。
滑动窗口枚举
不出意外,这个又超时了
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
def multiply(num_list): # 累乘
res = 1
for num in num_list:
res *= num
return res
num = 0
for size in range(1, len(nums) + 1):
for i in range(len(nums) + 1 - size):
sum_window = multiply(nums[i: i + size])
if sum_window < k:
num += 1
return num
双指针
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
length = len(nums)
num = 0
left = 0
sum = 1
for right, value in enumerate(nums):
sum *= value
while sum >= k and left <= right:
sum /= nums[left]
left += 1
num += right - left + 1
return num
以 nums = [10,5,2,6], k = 100 为例:
right = 0
[10] 5 2 6 sum < k num += 0-0+1 = 1 [10]
right = 1
[10 5] 2 6 sum < k num += 1-0+1 = 2 [5] [10 5]
right = 2
[10 5 2] 6 sum = k
10 [5 2] 6 sum < k num += 2-1+1 = 2 [2] [5 2]
right = 3
10 [5 2 6] sum < k num += 3-1+1 = 3 [6] [2 6] [5 2 6]

边栏推荐
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
- 云图说丨初识华为云微服务引擎CSE
- Line the last time the JVM FullGC make didn't sleep all night, collapse
- 【C语言学习笔记(五)】while循环与for循环
- idea——同一项目开启多个实例(不同端口)
- Postgresql源码(64)查询执行——子模块Executor(2)执行前的数据结构和执行过程
- docker mysql 容器中执行mysql脚本文件并解决乱码
- Handler source code analysis
- pg_memory_barrier_impl in Postgresql and C's volatile
- ctfshow php特性
猜你喜欢
随机推荐
Brush the topic of mobile zero power button
Web项目中简单使用线程池
LOL英雄联盟卡顿掉帧问题解决办法 2022年8月1日
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
Interview Blitz: What Are Sticky Packs and Half Packs?How to deal with it?
Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)
ECCV2022 | 用于视频问题回答的视频图Transformer
Unity gets the actual coordinates of the ui on the screen under the canvas
Solution for no navigation bar after Word is saved as PDF
Execute the mysql script file in the docker mysql container and solve the garbled characters
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
系统太多,多账号互通如何实现?
Unity获取canvas 下ui 在屏幕中的实际坐标
Compose原理-compose中是如何实现事件分法的
图像超分——Real-ESRGAN快速上手
Don't look down upon the WebSocket!Long connection, stateful, two-way, full-duplex king is Fried
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
虚拟机vmware设置桥接模式上网
金鱼哥RHCA回忆录:CL210管理计算资源--管理计算节点+章节实验
Postgresql-xl global snapshot and GTM code walking (branch line)







![[Notes] Introduction to machine learning](/img/69/e2acd3efd5f513c9c32fca701b66c0.png)

