[LeetCode] Subarray Product Less Than K 子数组乘积小于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.
这道题给了我们一个数组和一个数字K,让求子数组且满足乘积小于K的个数。既然是子数组,那么必须是连续的,所以肯定不能给数组排序了,这道题好在限定了输入数字都是正数,能稍稍好做一点。博主刚开始用的是暴力搜索的方法来做的,就是遍历所有的子数组算乘积和K比较,两个 for 循环就行了,但是 OJ 不答应。于是上网搜大神们的解法,思路很赞。相当于是一种滑动窗口的解法,维护一个数字乘积刚好小于k的滑动窗口,用变量 left 来记录其左边界的位置,右边界i就是当前遍历到的位置。遍历原数组,用 prod 乘上当前遍历到的数字,然后进行 while 循环,如果 prod 大于等于k,则滑动窗口的左边界需要向右移动一位,删除最左边的数字,那么少了一个数字,乘积就会改变,所以用 prod 除以最左边的数字,然后左边右移一位,即 left 自增1。当确定了窗口的大小后,就可以统计子数组的个数了,就是窗口的大小。为啥呢,比如 [5 2 6] 这个窗口,k还是 100,右边界刚滑到6这个位置,这个窗口的大小就是包含6的子数组乘积小于k的个数,即 [6], [2 6], [5 2 6],正好是3个。所以窗口每次向右增加一个数字,然后左边去掉需要去掉的数字后,窗口的大小就是新的子数组的个数,每次加到结果 res 中即可,参见代码如下:
class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
if (k <= ) return ;
int res = , prod = , left = ;
for (int i = ; i < nums.size(); ++i) {
prod *= nums[i];
while (left <= i && prod >= k) prod /= nums[left++];
res += i - left + ;
}
return res;
}
};
讨论:这道题其实可有很多种变形,比如当数组的数字有负数和0该怎么做?或者求的不是子数组,而是子序列该怎么做,子序列的话就可以排序啦,当然还是需要都是正数,才有排序的意义,博主觉得如果有负数和0,是不是只能暴力破解了,或者使用 Maximum Product Subarray 中的方法?再有一种的变形就是求子数组或子序列乘积刚好等于k,这就跟 Subarray Sum Equals K 和 Maximum Size Subarray Sum Equals k 这两题中使用的方法类似吧,建立子数组和其乘积之间的映射来快速找到。
欢迎大家在评论区留言讨论!
Github 同步地址:
https://github.com/grandyang/leetcode/issues/713
类似题目:
Maximum Size Subarray Sum Equals k
参考资料:
https://leetcode.com/problems/subarray-product-less-than-k/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Subarray Product Less Than K 子数组乘积小于K的更多相关文章
- 动态规划-子数组乘积小于k的总个数 Subarray Product Less Than K
2018-09-01 23:02:46 问题求解: 问题求解: 最开始的时候,一眼看过去就是一条 dp 嘛,保存每个数字结尾的长度和,最后求和就好,至于长度如何求,本题中需要用滑动窗口来维护. 很好的 ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [LeetCode] 560. Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [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]523. Continuous Subarray Sum连续子数组和(为K的倍数)
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)
713. 乘积小于K的子数组 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解 ...
- 【python-leetcode713-双指针】乘积小于k的子数组
问题描述: 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100输出: 8解释: 8个乘积小于10 ...
- JZ009乘积小于k的子数组
title: 乘积小于k的子数组 题目描述 题目链接:乘积小于k的子数组.剑指offer009 解题思路 注意: 一开始的乘积k值就是小的,随着右边窗口移动才会不断增大 怎么样的条件才能更新左窗口:当 ...
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
随机推荐
- Oracle查看表空间大小和使用率
1. 全部表空间的大小select tablespace_name, sum(bytes)/1024/1024 from dba_data_files group by tablespace_name ...
- 【RabbitMQ系列】 Spring mvc整合RabbitMQ
一.linux下安装rabbitmq 1.安装erlang环境 wget http://erlang.org/download/otp_src_18.2.1.tar.gz tar xvfz otp_s ...
- C语言第三次作业总结
本次作业的亮点 总体情况 大部分同学基本掌握了单层循环结构的写法,懂得了代码调试的过程 PTA通过率及作业质量都不错,希望再接再厉 推荐博客 黄毓颖 推荐理由:代码思路清晰,格式良好:调试过程相当形象 ...
- 如何减小ios安装包大小
以前的老文章了,搬到cnblog 更小的安装包意味着更快的下载安装速度,也往往意味着更快的加载运行速度,是优化ios应用的一个重要方面,本文主要参考<减小iOS应用程序的大小>,在实际测试 ...
- iOS开发-简单的循环结构分析
1.while循环 while (循环条件) { 循环体: } // 1.定义循环变量 ; // 2.循环条件 ) { // 3.循环体 printf("%d\n" ...
- CentOS7安装配置iptables防火墙
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761 CentOS7默认的防火墙不是iptables,而是firewall ...
- JS中的 map, filter, some, every, forEach, for...in, for...of 用法总结
1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5]; let other = list.map((d, i) => { ...
- linux cenots7安装mysql
1.下载mysql 下载的话先确认好版本. system:centos7 mysql:5.7 下面的版本自己选择,一般是86位的. 下载好的文件 2.上传到服务器 soft文件夹,终端也进入了 ...
- 第5章 子网划分和CIDR
第5章 子网划分和CIDR 划分网络 根据A类.B类或C类网络ID来识别网段具有一些局限性,主要是在网络级别之下不能对地址空间进行任何逻辑细分 如果一个IP是一个A类网络.数据报到达网关,然后传输到9 ...
- Hadoop2.6.0实践:001 伪分布式环境搭建
##################### Centos6.4VM_01_os.rar ################################################准备工作/opt ...