713. Subarray Product Less Than K
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
.
Approach #1. Math. [Java]
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int pro = 1;
int cnt = 0; for (int i = 0, j = 0; j < nums.length; ++j) {
pro *= nums[j];
while (i <= j && pro >= k) {
pro /= nums[i++];
}
cnt += j - i + 1;
} return cnt;
}
}
Analysis:
The idea is always keep an max-product-window less than K;
Every time shift window by adding a new number on the right(j), if the product is greater than K, then try to reduce numbers on the left(i), untill the subarray product fit less than K again, (subarray could be empty);
Each step introduces x new subarrays, where x is the size of the current window (j - i + 1);
Example:
for window (5, 2), when 6 is ntroduced, it add 3 new subarray: (5, (2, (6)))
Reference:
https://leetcode.com/problems/subarray-product-less-than-k/discuss/108861/JavaC%2B%2B-Clean-Code-with-Explanation
713. Subarray Product Less Than K的更多相关文章
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- [leetcode] 713. Subarray Product Less Than K
题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...
- Subarray Product Less Than K LT713
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [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 ...
- LeetCode Subarray Product Less Than K
原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...
- Subarray Product Less Than K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- leetcode713 Subarray Product Less Than K
""" Your are given an array of positive integers nums. Count and print the number of ...
- LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...
随机推荐
- discuz模板引擎语法
论坛的首页模板:forum/discuz.htm 版块的内容模板:forum/forumdisplay.htm 主题的查看模板:forum/viewthread.htm 帖子的内容模板:forum/p ...
- C语言文本处理
一.conf文本 http://blog.163.com/lixiangqiu_9202/blog/static/53575037201431743236762/ http://blog.csdn.n ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 工作五年以上的 UI 设计师都在干什么?
30 岁,现在坐标北京,从毕业至今都一直在做设计.目前从业超过了五年,也没打算离开设计这个行业.即便有一天不再从事设计专职的岗位,也仍然会在生活中,或者一些份外的工作中做「设计师」的角色,因为设计已成 ...
- hdu-1209(细节题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1209 注意:1.时钟到12要变为0 2.注意比较角度相同的情况 #include<iostrea ...
- [转]Go与C语言的互操作
Go有强烈的C背景,除了语法具有继承性外,其设计者以及其设计目标都与C语言有着千丝万缕的联系.在Go与C语言互操作(Interoperability)方面,Go更是提供了强大的支持.尤其是在Go中使用 ...
- Linux下Git安装及配置
转载自:https://blog.csdn.net/u013256816/article/details/54743470:加了一些自己的注释. yum安装 这里采用的是CentOS系统,如果采用yu ...
- b2_trsd_EDSD_new
# -*- coding:utf-8 -*- import re ss="./data/" year = '17A' filename = ss+'EDSD%s.txt'%year ...
- 高翔《视觉SLAM十四讲》从理论到实践
目录 第1讲 前言:本书讲什么:如何使用本书: 第2讲 初始SLAM:引子-小萝卜的例子:经典视觉SLAM框架:SLAM问题的数学表述:实践-编程基础: 第3讲 三维空间刚体运动 旋转矩阵:实践-Ei ...
- day08(异常处理,创建异常,finally,throws和throw的区别)
异常处理, 异常的产生 运行时异常:程序运行中产生的异常:RuntimeException类. 编译时异常:程序在编译时产生的异常:除了RuntimeException类 其他都是编译时产生的 ...