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]. 我们让这个子数组尽 ...
随机推荐
- c++之boost share_ptr
转载:https://www.cnblogs.com/welkinwalker/archive/2011/10/20/2218804.html
- HTML5 history详解
最近研究vue-router单页转跳而不向服务器请求的原理, 主要是HTML5 history以及hash的应用,支持history时使用history模式 下面详细学习了一下常用的history相关 ...
- 二进制搭建kubernetes多master集群【二、配置flannel网络】
上一篇我们已经搭建etcd高可用集群,参考:二进制搭建kubernetes多master集群[一.使用TLS证书搭建etcd集群] 此文将搭建flannel网络,目的使跨主机的docker能够互相通信 ...
- 2018.09.02 Atcoder Regular Contest 102简要题解
比赛传送门 T1 Triangular Relationship 分析之后发现有两种情况: 1. n为奇数,那么所有数都是k的倍数. 2. n为偶数,那么所有数都是k/2的倍数. 然后就可以愉快A题了 ...
- Linux服务器部署系列之一—Apache篇(上)
Linux系统的应用越来越广泛了,学习linux系统的网管兄弟也有增加的趋势.很久以前就有些想法,要将自己学的linux知识整理一下.最近,终于下定决心,挤出时间开始动手写些东西了.虽然不一定好,不过 ...
- 20155216 2016-2017-2 《Java程序设计》第六周学习总结
20155216 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 流与IO 将数据从来源中取出,可以使用输入串流:将数据写入目的地,可以使用输出串流,串流是有 ...
- VS2013支持多字节的方法
参考链接: https://jingyan.baidu.com/article/6181c3e06ab30f152ff1534d.html
- js基础学习笔记(五)
多种选择(Switch语句) 当有很多种选项的时候,switch比if else使用更方便. 语法: switch(表达式) { case值1: 执行代码块 1 break; case值2: 执行代码 ...
- (匹配)Oil Skimming -- hdu --4185
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4185 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 网络中路由器相关的名词理解LAN,WAN,WLAN
1. LAN,全称Local Area Network,中文名叫做局域网. LAN是指在某一区域内由多台计算机互联成的计算机组.一般是方圆几千米以内. 局域网可以实现文件管理.应用软件共享.打印机共享 ...