Subarray Product Less Than K LT713
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.
Idea 1. Brute force, any subarray nums[i..j] can be represented by a given pair of integers 0 <= i <= j < nums.length.
Time complexity: O(n2)
Space complexity: O(1)
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int count = 0;
for(int i = 0; i < nums.length; ++i) {
int product = 1;
for(int j = i; j < nums.length; ++j) {
product *= nums[j];
if(product < k) {
++count;
}
else break;
}
}
return count;
}
}
Idea 2. Binary search. Taking advantage:
1. log(x*y) = log(x) + log(y)
2. positive numbers mean monoto increasing for subarray product(or log subarray sum), the prefix product/sum is sorted
reducing subarry product problem to subarray sum problem. For each index i, find the rightmost index j such that prefix[j] - nums[i-1] < Math.log(k). Note the comparison for double, reduce log(k) by 1e-9, to avoid searching the wrong half.
Take nums= {10,3,3,7,2,9,7,4,7,2,8,6,5,1,5}, k = 30 for example,
when searching the subarray {6, 5, 1, 5}, 6* 5 * 1 == 30, if not adding 1e-9, the search will move to {5} because of doble comparison, hence make logK slightly smaller, if the product is equal to k, move to the lower half.
Time complexity: O(nlogn)
Space complexity: O(n)
class Solution {
int findMaxIndex(double[] prefix, int left, int right, int k) {
double logK = Math.log(k);
int i = left, j = right;
while(i < j) {
int mid = i + (j - i) / 2;
if(prefix[mid] - prefix[left-1] < logK - 1e-9) i = mid + 1;
else j = mid;
}
return i;
}
public int numSubarrayProductLessThanK(int[] nums, int k) {
int count = 0;
double[] prefix = new double[nums.length + 1];
for(int i = 1; i < prefix.length; ++i) {
prefix[i] = prefix[i-1] + Math.log(nums[i-1]);
}
for(int i = 1; i < prefix.length; ++i) {
int maxIndex = findMaxIndex(prefix, i, prefix.length, k);
count += maxIndex - i;
}
return count;
}
}
Idea 3. Slicing widnow. Keeping a max-product-windown less than k. Since the elements in the array is positive, the subarray product is a monotone increasing fuction, so we use a sliding window.
Time complexity: O(n)
Space complexity: O(1)
3.a Fixed the left point of a subarray, for each left, find the largest right so that the product of the subarray nums[left] + nums[left+1] + ... + nums[right-1] is less than k. Hence the product of each subarray starting at left and ending at rightEnd (left <= rightEnd < right) is less than k, there are right - left such subarrays. For every left, we update right and prod to maintain the invariant.
Starting with index left ( left = 0), find the first largest right so that the product * nums[right] is larger than k, add right - left to the count.
Shifting the window by moving left to the right by element, update product (product = product/nums[left]), the new subarry will start with index left = left + 1,
if the product*nums[right] is still larger than k, there are right - left subarrys with product less than k; Note: if right == left, it's empty array, no need to update product, product should remain as 1, it means nums[right] > k, we need to move right forward to maintain the invariant left <= right.
otherwise, expand right until product * nums[right] > k, continue the above process until left reaches the end of the array.
Take nums = [10, 5, 2, 100, 6], k = 100 for example,
left = 0, right = 2, product = 100, [10, 5, 2] , there are 2 (right - left) subarrays starting at 0 with product less than 100: [10], [10, 5]
left moving forward, left = 1, product = 100/nums[0] = 10, continue expand right until product = 1000 >= k, right = 3, [5, 2, 100], 2 (right - left) subarrays starting with 5: [5], [5, 2]
left moving forward, left = 2, product = 1000/nums[1] = 200, since product is already larger than k, no need to expand right, keep right= 3, 1 (right - left) subarray starting with 2: [2]
left moving forward, left = 3, right = 3, since left < right, as we define right as the excluded boundary, there is no subarry in the range (right - left) = 0, right is not in the product, we need to move right, right = 4
left moving forward, left = 4, product = 1, 1 * 6 < 100, expanding right = 5, 1 subarray starting with 6, [6]
(10)
(10, 5, 2)
(5, 2, 100)
(2, 100)
(100)
(6)
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int count = 0;
int product = 1;
for(int left = 0, right = 0; left < nums.length; ++left) {
for(;right < nums.length && product * nums[right] < k; ++right) {
product *= nums[right];
}
count += right - left;
if(left < right) {
product = product/nums[left];
}
else {
++right;
}
}
return count;
}
}
3.b Fix the right point, for each right point, find the smallest left such that the product is less than k.
Shifting the window by adding a new element on the right,
if the product is less than k, then all the subarray nums[left...right] satisfy the requrement, there are right - left + 1 such subarrays ending at index right;
otherwise, shrinking the subarray by moving left index to the right until the subarray product less than k; Note if nums[right] >= k, left will move one step before right, the arry ending at right is empty with right - left + 1 = 0.
(10)
(10, 5)
(5, 2)
(100) right = left + 1, count = 0
(6)
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int count = 0;
int product = 1;
for(int left = 0, right = 0; right < nums.length; ++right) {
product *= nums[right];
while(left <= right && product >= k) {
product /= nums[left];
++left;
}
count += right - left + 1;
}
return count;
}
}
Subarray Product Less Than K LT713的更多相关文章
- [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 ...
- 713. 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】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 ...
- LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...
随机推荐
- 《xss跨站脚本剖析与防御》实验笔记
1.书籍<xss跨站脚本剖析与防御>上介绍的xss测试代码 <img src="javascrpt:alert('xss');">, <table b ...
- SQL Server 2008 R2官方中文版下载
SQL Server 2008是一个重大的产品版本,它推出了许多新的特性和关键的改进,使得它成为至今为止的最强大和最全面的SQL Server版本. 在现今数据的世界里,公司要获得成功和不断发展,他们 ...
- WAS 添加数据源
一.创建安全性别名认证 1.资源-全局安全性-JAVA认证和授权服务-J2C认证数据 2.新建 3.输入别名,这里后面加IP末尾.输入用户名.密码. 4.点击确定.保存. 二.创建数据源连接配置 1. ...
- HDU-1069.MonkeyandBanana(LIS)
本题大意:给出n个长方体,每种长方体不限量,让你求出如何摆放长方体使得最后得到的总高最大,摆设要求为,底层的长严格大于下层的长,底层的宽严格大于下层的宽. 本题思路:一开始没有啥思路...首先应该想到 ...
- AD操作
加泪滴 批量添加覆铜过孔(先铺铜以后,再批量添加过孔) 开槽 在KEPP—OUT层 部分区域 不敷铜 开窗
- ES5之函数的间接调用 ( call、apply )、绑定 ( bind )
call().apply()的第一个实参是函数调用的上下文,在函数体内通过this来获得对它的引用. call()将实参用逗号分隔:apply ()将实参放入数组.类数组对象中. function h ...
- [Java笔记]面向对象-单例模式
单例模式 目标 使JVM中最多只有一个该类的实例,以节省内存.缺点:只能建一个该类的实例. 实现 具体实现思路: 1构造方法私有化//故在外面不能new很多次 2对外提供一个公开的静态的类方法,获取类 ...
- Delphi: RTTI与ini配置文件
项目以Rtti特性做文件参数配置,简化每项读写ini操作,记录以做备忘,代码如下: unit uGlobal; interface uses Windows, Messages, SysUtils, ...
- Delphi: TLabel设置EllipsisPosition属性用...显示过长文本时,以Hint显示其全文本
仍然是处理多语言中碰到问题. Delphi自2006版以后,TLabel有了EllipsisPosition属性,当长文本超过其大小时,显示以...,如下图: 这样虽然解决显示问题,但很显然,不知道. ...
- Delphi中记录体做为属性的赋值方法
1. 起源 此问题源于[秋风人事档案管理系统]用Delphi XE重编译中所发现. 快十年了,当初Delphi 7所编写项目,想用Delphi XE重新编译,并打算做为Free软件发布,编译错误中发现 ...