leetcode713 Subarray Product Less Than K
"""
Your are given an array of positive integers nums.
Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is 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.
"""
"""
正确做法,双指针加滑动窗口
很是巧妙
pro为乘积,pro不断乘以nums[j]得到子数组乘积,满足条件小于k时
!!!
res +=(j-i+1),res是计数器。
滑动窗口内元素间的相互组合的数量
"""
class Solution1:
def numSubarrayProductLessThanK(self, nums, k):
res, i = 0, 0
pro = 1 #乘积
if k <= 1: # 对于案例nums=[1, 2, 3] k=0 和 nums=[1, 1, 1] k=1
return 0
for j in range(len(nums)):
pro = pro * nums[j]
while pro >= k:
pro = pro // nums[i]
i += 1
res += j - i + 1 #!!!这个很关键
return res """
我的解法超时,
对于输入nums = [1, 1, 1, 1......,1] k=5
"""
class Solution2:
def numSubarrayProductLessThanK(self, nums, k):
res = 0
for i in range(len(nums)):
pro = 1
j = i
while j < len(nums):
if nums[j] * pro < k:
res += 1
pro = nums[j] * pro
j += 1
else:
break
return res
leetcode713 Subarray Product Less Than K的更多相关文章
- Subarray Product Less Than K LT713
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [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 ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于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 ...
- LeetCode Subarray Product Less Than K
原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- [leetcode] 713. Subarray Product Less Than K
题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...
- LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...
随机推荐
- 使用vASA842配置ASDM645
准备:使用VMware打开vASA842.ova文件,将第一个网络适配器桥接到一个vmnet接口,我这里是vmnet3,网段是10.0.0.0/24,网关是10.0.0.1/24 1.打开vASA84 ...
- 【转】使用shell登录远程服务器执行多条命令,ssh登录之后执行脚本文件
原文:https://blog.csdn.net/qq_36622490/article/details/100773589 这个需求主要是我在jenkins中pipeline的代码里,需要使用she ...
- c语言中的qsort用法
1.首先了解 int cmp(const void* a, const void* b) 表示声明cmp函数,其返回值为int型,参数为两个不可修改(const)的void型指针 2.函数原型 函数声 ...
- Update(stage3):第1节 redis组件:8、主从复制架构;9、Sentinel架构
8.redis的主从复制架构 在Redis中,用户可以通过执行SLAVEOF命令或者设置slaveof选项,让一个服务器去复制(replicate)另一个服务器,我们称呼被复制的服务器为主服务器(ma ...
- 解决RStudio(非conda安装)在使用Anaconda中的R环境时,缺失“ libbz2-1.dll ”而不能正常启动问题
1.问题描述 当非conda安装的RStudio,在调用Anaconda中的R环境时,报如下错误: 2.解决办法 下载同版本的R,对Anaconda中R相应的文件进行替换(图标中标注的部分) R3.5 ...
- Git fork后如何同步源仓库更新
1. 设置源仓库的远程地址 >> git remote add [新地址名称] [源仓库远程地址] >> git remote add upstream https://git ...
- PAT T1022 Werewolf
暴力搜索加剪枝~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; bool visit[maxn]; vector& ...
- day3-1函数
函数: 如果写在对象内,是一个方法 函数声明 function 函数名(形参列表){ //函数体 } 函数表达式 var 函数名 = function (形参列表){ //函数体 } 匿名函数 f ...
- tensorflow文本分类实战——卷积神经网络CNN
首先说明使用的工具和环境:python3.6.8 tensorflow1.14.0 centos7.0(最好用Ubuntu) 关于环境的搭建只做简单说明,我这边是使用pip搭建了python的 ...
- Microsoft Cortana移动版除美国市场外不再可用
导读 先前已经透露,Microsoft Cortana的移动版本已不复存在.目前,Microsoft Cortana在移动设备上的多个国家和地区中支持多种语言.微软的Cortana移动版本不再支持的市 ...