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]. 我们让这个子数组尽 ...
随机推荐
- Macbook pro睡眠状态恢复后没声音的解决办法
杀招: sudo killall coreaudiod macos会自动重启进程,恢复声音
- ubuntu下常用操作
屏幕截图: 可以用ubuntu自带的截图软件:gnome-screenshot. 该工具截图区域并且复制到剪切板命令为 gnome-screenshot -c -a,可以给该命令添加快捷方式,alt ...
- 编译安装bluez5.44
1.下载 2. configure 提示需要glib 3.yum install glib 4.还是提示glib 5.yum install glib-devel 下载编译glib make inst ...
- [BAT]批处理脚本双击可运行,但在定时计划任务中无法执行(当前工作路径不对导致的)
一开始,红色部分我是用set AutoPath=%cd%,双击可执行,但是将这个批处理脚本放在定时任务中无法执行,后来发现在定时执行的时候,当前工作路径不是批处理脚本所在的路径,而是C:/Window ...
- Python3 urllib库和requests库
1. Python3 使用urllib库请求网络 1.1 基于urllib库的GET请求 请求百度首页www.baidu.com ,不添加请求头信息: import urllib.requests d ...
- cocos2d接安卓facebook插件(已测cocos-x 3.7 3.8版本)
1 控制台创建新工程: a 控制台 进入cocos2d文件夹下面,如cocos2d-x-3.7.1,执行setup.py,未设置NDK SDK ANT 路径的设置路径,需要改路径的 explore ...
- DB2分页查询SQL
select * from (select row_number() over() as rown,tpag.* from(SELECT int(COALESCE(列名1,0)),COALESCE(列 ...
- 详解Windows Service Wrapper(winsw.exe)及应用场景
winsw.exe可以帮助nginx作为windows服务自启动,不需要每次都输入命令,很方便,使用到目前为止这种做法的效果很完美.你得到了 Windows 服务的支持,而且在服务重启时没有遗留孤立的 ...
- xampp虚拟主机的配置
ps:来源 https://blog.csdn.net/qq_17335153/article/details/52091869 一.修改httpd.conf 文件目录 xampp => ...
- Python特殊方法
# __slots__如果要限制添加的属性,例如,Student类只允许添加 name.gender和score 这3个属性,就可以利用Python的一个特殊的__slots__来实现. # __sl ...