"""
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. 吴裕雄--天生自然TensorFlow2教程:损失函数及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

  2. 进程,内存,管理 ps,pstree,top,free,vmstat,iftop,lsof,查看网速

    一些基础 不同进程之间,进行数据访问 同一主机:pipe 管道 socket   套接字文件 signal   信号 shm   shared memory semaphore 信号量,一种计数器 不 ...

  3. Cisco AP-Sniffer模式空口抓包

     第一步:WLC/AP侧 配置AP为sniffer模式: 配置提交后,AP会重启,并且将不能发出SSID为clients提供服务. 第二步:一旦AP重新加入WLC,配置AP抓取的信道和抓取后的数据包发 ...

  4. 杭电2070 Fibbonacci Number

    Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. __str__()方法和__repr__()方法

    有时候我们想让屏幕打印的结果不是对象的内存地址,而是它的值或者其他可以自定义的东西,以便更直观地显示对象内容,可以通过在该对象的类中创建或修改__str__()或__repr__()方法来实现(显示对 ...

  6. 刚开始用springboot踩的好多坑!!!

    今天,刚开始就在刚才我留下了激动的泪水,因为我捯饬springboot已经有几天了,我通过看视频学的,但是坑实在是太多了,今年是鼠年~~~LOL----瘟疫之源来了, 被困在了老家不能走,老家网实在是 ...

  7. location练习!(重点)

    写location最重要的就是hosts文件,添加一次域名跳转就得添加一条hosts文件 hosts文件: 192.168.200.120 www.a.com 192.168.200.119 www. ...

  8. 「JSOI2014」支线剧情2

    「JSOI2014」支线剧情2 传送门 不难发现原图是一个以 \(1\) 为根的有根树,所以我们考虑树形 \(\text{DP}\). 设 \(f_i\) 表示暴力地走完以 \(i\) 为根的子树的最 ...

  9. selenium+chrome options

    selenium+chrome options 环境:selenium chrome 1.      selenium + chrome参数配置 1.1.    启动 from selenium im ...

  10. 笔记-AJAX

    笔记-AJAX 1.      简介 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML AJAX 是一种用于创建快速动态网页的技术 ...