"""
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的更多相关文章

  1. Subarray Product Less Than K LT713

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  2. [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 ...

  3. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  4. 713. Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  5. LeetCode Subarray Product Less Than K

    原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...

  6. Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  7. 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...

  8. [leetcode] 713. Subarray Product Less Than K

    题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...

  9. LeetCode Subarray Product Less Than K 题解 双指针+单调性

    题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...

随机推荐

  1. CSS shapes布局教程

    文章参考至 一.前言&索引 CSS Shapes布局可以实现不规则的文字环绕效果,需要和浮动配合使用. 兼容性如下图: 还是很不错的,移动端可用,内部中后台项目可用. CSS shapes布局 ...

  2. C语言-调试

    1 格式化输出函数printf("%d %s",a,str):格式化控制符之间不能有“逗号”,可以用空格 1.1格式化输入函数scanf(“%d”,t)格式化控制符之间不能有空格 ...

  3. spring boot整合Thymeleaf

    1.引入thymeleaf: <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  4. 如何去掉Eclipse注释中英文单词的拼写错误检查

  5. windows下 DEV-C++无法连接到pthread.h的解决办法

    参考的这个博文,原博文有图片:http://lslin.iteye.com/blog/776325 (我只是为了方便写.copy一遍) dev-C++编写C/C++程序时,非常方便轻巧,但是今天学习多 ...

  6. gradle 打包所有依赖 Invalid signature file digest for Manifest main attributes(转)

    1.打包所有依赖: // 指定main函数的类 jar { manifest { attributes "Main-Class": "com.baeldung.fatja ...

  7. RESTful风格化

    RESTful Web Service介绍 Roy Thomas Fielding博士2000年提出的 REST是英文Representational State Transfer的缩写 表象化状态转 ...

  8. centos7一步一步搭建docker jenkins 及自定义访问路径重点讲解

    系统环境:centos7.7 (VMware中) 镜像image 版本:jenkins/jenkins (截止2020.01.10最新版) 参考文章:https://www.jianshu.com/p ...

  9. Number()、parseInt()、parseFloat()、~~、~

    一.Number() 如果是Boolean值,true和false值将分别被转换为1和0. 如果是数字值,只是简单的传入和返回. 如果是null值,返回0. 如果是undefined,返回NaN. 如 ...

  10. SignalR Connection has not been fully initialized

    在使用 SignalR 过程中遇到 SignalR: Connection has not been fully initialized. Use .start().done() or .start( ...