原题链接在这里: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. Linux实用命令工具-dtrx根据需要自动解压

    刚刚逛网站的时候看到一个命令工具很不错——dtrx. 这个工具能够解压的类型包括tar, zip,rpm, deb, gem, 7z, cpio, rar 等等,并且这个工具能自动识别压缩包类型并进行 ...

  2. Oracle 伪列

    ROWNUM ROWNUM:表示行号,实际上此是一个列,但是这个列是一个伪列,此列可以在每张表中出现. 范例:在查询雇员表上,加入 ROWNUM SELECT ROWNUM,empno,ename,j ...

  3. sql server 数据库复制实现数据同步常见问题(不定期更新)

    sql server2008数据库复制实现数据同步常见问题 在原作者基础上追加 sql server2008数据库复制实现数据同步常见问题 23.发布 'xx' 的并发快照不可用,因为该快照尚未完全生 ...

  4. Spring中Bean管理的常用注解

    在Spring中,主要用于管理bean的注解分为四大类:1.用于创建对象.2.用于给对象的属性注入值.3.用于改变作用的范围.4.用于定义生命周期.这几个在开发中经常接触到,也可以说每天都会遇见.其中 ...

  5. 2-Color Dutch National Flag Problem

    2-Color Dutch National Flag Problem 问题 a[0..n-1]中包含红元素或蓝元素;重新放置使得 红元素均在蓝元素之前. 循环不变式 每一次循环,a[0...k-1] ...

  6. git原理学习

    http://git.oschina.net/progit/   这一点值得牢记:Git 会把工作目录的内容恢复为检出某分支时它所指向的那个提交对象的快照.它会自动添加.删除和修改文件以确保目录的内容 ...

  7. EF Code-First 学习之旅 多对多的关系

    public class Student { public Student() { this.Courses = new HashSet<Course>(); } public int S ...

  8. spark学习12(Wordcount程序之spark-shell)

    在目录/home/hadoop/2016113012下有文件words.txt hello scala hello java hello python hello wujiadong 上传该文件到hd ...

  9. oracle:与mysql相似得find_set_in函数用法

    Oracle中实现find_in_set CREATEORREPLACEFUNCTION FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep ...

  10. 初试Orchard Core CMS

    关于Orchard Core CMS,这是一套内容管理系统(Content Management System),看一下来自官方文档的解释,什么是Orchard CMS. Orchard is a f ...