原题链接在这里: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.

题解:

都是正数, 所以乘积prod是递增的.

使用sliding window, sliding window prod小于k时右侧前进, 大于等于k时左侧前进.

维护计数. 右侧减左侧加一 表示以当前右侧为尾的subarray个数.

Time Complexity: O(nums.length).

Space: O(1).

AC Java:

 class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if(nums == null || nums.length == 0 || k <= 1){
return 0;
} int res = 0;
int prod = 1;
int l = 0;
int r = 0;
while(r < nums.length){
prod *= nums[r];
while(prod >= k){
prod /= nums[l++];
}
res += r-l+1;
r++;
} return res;
}
}

类似Subarray Sum Equals K.

LeetCode Subarray Product Less Than K的更多相关文章

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

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

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

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

  3. Subarray Product Less Than K LT713

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

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

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

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

  6. 713. Subarray Product Less Than K

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

  7. Subarray Product Less Than K

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

  8. leetcode713 Subarray Product Less Than K

    """ Your are given an array of positive integers nums. Count and print the number of ...

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

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

随机推荐

  1. 如何编写自己的虚拟DOM

    要构建自己的虚拟DOM,需要知道两件事.你甚至不需要深入 React 的源代码或者深入任何其他虚拟DOM实现的源代码,因为它们是如此庞大和复杂--但实际上,虚拟DOM的主要部分只需不到50行代码. 有 ...

  2. nginx日志分割总结

    nginx日志自己不会进行分个,所有日志都会累积的记录在 access.log,error.log 中,当请求量大,一天就能到几百兆,如果不进行分给,对日志的查看和写入性能都有影响. 1. 编写脚本n ...

  3. Python MySQL数据库连接模块

    1. MySQLdb只支持在Python 2版本使用MySQLdb是用于Python链接Mysql数据库的接口.a.pip安装 直接使用pip进行安装,在此之前需要安装一些系统依赖包. ● CentO ...

  4. Mysql导出数据结构 or 数据

    如果我们单单只想导出mysql数据表结构,通过navcat工具还不行,这时我们可以用mysqldump工具 在mysql server的安装目录:C:\Program Files\MySQL\MySQ ...

  5. Activity启动的四种方式

    Activity启动方式有四种,分别是: standardsingleTopsingleTasksingleInstance 可以根据实际的需求为Activity设置对应的启动模式,从而可以避免创建大 ...

  6. DelayQueue与ProirityBlockingQueue

    DelayQueue是一个无界队列,只有在延迟期满的时候,才可以取出元素.该队列的头部存储的延期期满了后保存时间最长的元素. DelayQueue阻塞队列在我们系统开发中也常常会用到,例如:缓存系统的 ...

  7. JMS-activeMq发布订阅模式

    上一篇对点对点模式进行总结,这一篇都发布订阅模式进行总结,代码都差不多,唯一区别就是创建队(session.createQueue(目的地))列改为创建主题(session.createTopic(目 ...

  8. 4.微信小程序-B站:先把首页造出来

    (小安娜:失踪人口已上线,大家快来喷喷喷他!),sorry++,最近身边发生太多事情,导致这最关键的实战开篇都未写,(小安娜-分身1:懒就是懒,不负责任,我之前学的都忘了),(小安娜-分身2:上一篇双 ...

  9. Java内存分析1 - 从两个程序说起

    这次看一些关于JVM内存分析的内容. 两个程序 程序一 首先来看两个程序,这里是程序一:JVMStackTest,看下代码: package com.zhyea.robin.jvm; public c ...

  10. 两种以太网 RDMA 协议: iWARP 和 RoCE

    本文是讲演 How Ethernet RDMA Protocols iWARP and RoCE Support NVMe over Fabrics[1]的摘要. 如果 NVMe 存储系统与主机是分离 ...