原题链接在这里: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. Hadoop:相关概念

    Hadoop:相关概念 一.Hadoop简介 Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS. 1.特点 (1)HDFS有高容错性的 ...

  2. spring boot应用测试框架介绍

    一.spring boot应用测试存在的问题 官方提供的测试框架spring-boot-test-starter,虽然提供了很多功能(junit.spring test.assertj.hamcres ...

  3. Kali更新源,亲测目前可用的源

    kali更新的时候老是无法定位软件包,网络上大部分中科大.阿里云kali源都不可用,都千篇一律,最后找了这个,网易的,还不错,贴出来大家看看: # 源 deb http://mirrors.163.c ...

  4. 20145109《Java程序设计》第一周学习总结

    20145109 <Java程序设计>第一周学习总结 教材学习内容总结 About JVM, JRE, JDK JVM包含于JRE中,用于运行Java程序.JDK用于开发Java程序,包含 ...

  5. protobuf与json转换

    protobuf对象不能直接使用jsonlib去转,因为protobuf生成的对象的get方法返回的类型有byte[],而只有String类型可以作为json的key,protobuf提供方法进行转换 ...

  6. python学习笔记(mysqldb下载安装及简单操作)

    python支持对mysql的操作 已经安装配置成功python.mysql 之后根据各自电脑配置选择对应系统的MySQL-python 文件是EXE格式.打开下一步即可 下载地址博主分享下: htt ...

  7. Python 数据分析练习1

    环境 Anaconda3 Python 3.6, Window 64bit 目的 从MySQL数据库读取数据,进行数据清理,数据展示 代码 # -*- coding: utf-8 -*- import ...

  8. 51nod 1042 数位dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042 1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131 ...

  9. C语言小知识点汇集

    1. 三元表达式“?:”冒号两边的两个操作数类型必须一致. 2. 枚举变量的sizeof值同int型一样为4. 3. 为了消除不必要的计算 &&两侧的表达式,当左侧即第1个表达式不成立 ...

  10. 【scala】继承

    Scala中的继承与Java有着显著的不同. 抽象类abstract class abstract class Car{//抽象类 val carBrand:String;//抽象字段,一个没有被初始 ...