作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/subarray-product-less-than-k/description/

题目描述:

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.

Note:

  • 0 < nums.length <= 50000.
  • 0 < nums[i] < 1000.
  • 0 <= k < 10^6.

题目大意

找出一个数组中连续子数组的乘积有多少个小于k的。

解题方法

因为只要求个数,不要求列出来,那么可以想到双指针。

这个题的做法还是很简单的,使用两个指针确定子数组的边界,然后求子数组的乘积,如果乘积大于等于k了,需要移动左指针。每次移动有指针之后,符合题目要求的结果增加的是以右指针为边界的子数组数量,也就是r - l + 1。

注意移动左指针的时候,不能超过右指针。

时间复杂度是O(N),空间复杂度是O(1)。

class Solution(object):
def numSubarrayProductLessThanK(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
N = len(nums)
prod = 1
l, r = 0, 0
res = 0
while r < N:
prod *= nums[r]
while l <= r and prod >= k:
prod /= nums[l]
l += 1
res += r - l + 1
r += 1
return res

参考资料:

日期

2018 年 10 月 14 日 —— 周赛做出来3个题,开心

【LeetCode】713. Subarray Product Less Than K 解题报告(Python)的更多相关文章

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

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

  2. 713. Subarray Product Less Than K

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

  3. 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...

  4. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  6. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  7. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  8. Subarray Product Less Than K LT713

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

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

随机推荐

  1. Docker Swarm的命令

    初始化swarm manager并制定网卡地址docker swarm init --advertise-addr 192.168.10.117 强制删除集群docker swarm leave -- ...

  2. 【模板】二分图最大权完美匹配(KM算法)/洛谷P6577

    题目链接 https://www.luogu.com.cn/problem/P6577 题目大意 给定一个二分图,其左右点的个数各为 \(n\),带权边数为 \(m\),保证存在完美匹配. 求一种完美 ...

  3. 12 — springboot集成JPA — 更新完毕

    1.什么是jpa? 一堆不想整在这博客里面的理论知识.这些理论玩意儿就应该自行领悟到自己脑海里 1).JPA & Spring Data JPA 1.1).JPA JPA是Java Persi ...

  4. Spark基础:(七)Spark Streaming入门

    介绍 1.是spark core的扩展,针对实时数据流处理,具有可扩展.高吞吐量.容错. 数据可以是来自于kafka,flume,tcpsocket,使用高级函数(map reduce filter ...

  5. Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec

    遇到这个一场折腾了1个小时, 这是系统在解析XML的时候出错, 最后费了好大的劲才发现 XML文件中,<View>  写成小写的 <view> 了. 崩溃啊.......... ...

  6. vue项目windows环境初始化

    下载nodejs zip包并加载到环境变量 nodejs的版本最好使用12版,而不是最新版 npm install webpack -gnpm install -g yarnyarn config s ...

  7. redis入门到精通系列(一)

    (一)为什么要用Nosql 如果你是计算机本科学生 ,那么一定使用过关系型数据库mysql.在请求量小的情况下,使用mysql不会有任何问题,但是一旦同时有成千上万个请求同时来访问系统时,就会出现卡顿 ...

  8. ActiveMQ(二)——ActiveMQ的安装和基本使用

    一:安装 2.启动之后成功 二.创建实例测试ActiveMQ 配置Maven所需的依赖 <dependency> <groupId>org.apache.activemq< ...

  9. 深入分析 Java ZGC

    传统的垃圾回收 CMS 与 G1 停顿时间瓶颈 ZGC 概览 深入 ZGC 原理 标记 Marking 着色指针 Reference Coloring Relocation 重映射和读屏障 Remap ...

  10. Mysql-5.6 二进制多实例部署

    目录 一.简介 二.环境声明 三.程序部署 一.简介 MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307),运行多个MySQL服务进程,通过不同的socket监听不同的服务 ...