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]. 我们让这个子数组尽 ...
随机推荐
- ES-倒排索引
一.什么事倒排索引 二.倒排索引为什么查询速度快 (1)ES全文检索的底层是使用倒排索引实现的 (2)倒排索引会将文档的内容进行关键词分词,可以使用关键词直接定位到文档的内容
- leetcode 0215
目录 ✅ 1002. 查找常用字符 描述 解答 java other's implementation(todo watch me) cpp py ✅ 821. 字符的最短距离 描述 解答 cpp p ...
- TP-LINK路由器端口映射全套教程(亲测有效)
最近想在自己的笔记本上搭建一个博客系统,方便自己写写日志,记录一些知识心得. 由于笔记本是长期放在家里的,需要在外边也能访问它,于是需要在路由器上设置一个端口映射,让在因特网上的其他电脑能访问到家里的 ...
- WCF 数据传输SIZE过大
1.当客户端调用WCF服务时,接受数据过大,可通过以下配置解决 <basicHttpBinding> <binding name="BasicHttpBinding_Wcf ...
- tomcat8配置了tomcat-users.xml,报403 Access Denied
配置了tomcat-users.xml之后,重启tomcat服务,仍然访问拒绝. 原因:tomcat8.5 更改之后,仍然访问拒绝. 还需步骤如下: vi /usr/local/tomcat/apac ...
- Keras的TimeDistributed层
Keras的TimeDistributed层主要用途是在时间维度上进行全连接. 比如Faster RCNN,1张图生成了16个ROI,需要对每一个ROI进行分类和回归,ROI的维度是7×7×512,长 ...
- Steam 游戏 《The Vagrant(流浪者)》修改器制作-[先使用CE写,之后有时间的话改用CheatMaker](2020年寒假小目标08)
日期:2020.02.07 博客期:146 星期五 [温馨提示]: 只是想要修改器的网友,可以直接点击此链接下载: 只是想拿CT文件的网友,可以直接点击此链接下载: 没有博客园账号的网友,可以将页面下 ...
- GO 回调实现
函数作为参数传递,实现回调. package main import "fmt" // 声明一个函数类型 type cb func(int) int func main() { t ...
- 汇编语言从入门到精通-5微机CPU的指令系统2
微机CPU的指令系统 5.2.2 标志位操作指令 标志位操作指令是一组对标志位置位.复位.保存和恢复等操作的指令. 1.进位CF操作指令 a.清进位指令CLC(Clear Carry Flag):CF ...
- tensorflow变量的使用(02-2)
import tensorflow as tf x=tf.Variable([1,2]) a=tf.constant([3,3]) sub=tf.subtract(x,a) #增加一个减法op add ...