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. 分析:https://www.cnblogs.com/grandyang/p/7753959.html
这道题给了我们一个数组和一个数字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(int[] nums, int k) {
if (k == ) return ;
int cnt = ;
int pro = ;
for (int i = , j = ; j < nums.length; j++) {
pro *= nums[j];
while (i <= j && pro >= k) {
pro /= nums[i++];
}
cnt += j - i + ;
}
return cnt;
}
}
Subarray Product Less Than K的更多相关文章
- 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 ...
- 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 ...
- 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]. 我们让这个子数组尽 ...
随机推荐
- 019_linuxC++之_函数模板引入
(一)首先我们来看非模板程序,函数只是输入不一样的变量就需要构件很多个不一样的函数,那么这样很麻烦,则引入函数模板 int& max(int& a, int& b) { ret ...
- wnmp安装配置的坑
1.下载安装nginx: nginx官网下载地址->下载nginx 下载任意版本.我下载的是1.13.12版本,解压到C:\wnmp\nginx(自己决定目录),运行nginx.exe. 访问h ...
- E. Intergalaxy Trips
完全图,\(1 \leq n \leq 1000\)每一天边有 \(p_{i,j}=\frac{A_{i,j}}{100}\) 的概率出现,可以站在原地不动,求 \(1\) 号点到 \(n\) 号点期 ...
- js面向对象学习笔记
1.函数的定义方式 第一种定义方式 function fn1() { alert("fn1"); } alert(fn) 函数就是一个特殊的对象,是一个Function类的实例,其 ...
- .net core Redis库 CSRedis
由于servicestack.redis收费,基于有人说StackExchange.Redis 使用会出现一些问题比如会超时, 找到了CSRedis这个库,很强大很实用.另外有兴趣的朋友还可以试试另一 ...
- codeforces685B
CF685B Kay and Snowflake 题意翻译 输入一棵树,判断每一棵子树的重心是哪一个节点. 题目描述 After the piece of a devilish mirror hit ...
- python 显示!到~的字符
count = ): != : print(chr(i),end=" ") else: print(chr(i)) count += 输出 ! " # $ % & ...
- 测试Promise与Async/await的基本使用
想在项目中用, 发现自己不是很熟 promise基本使用 基本使用-思路 new Promise()返回了一个状态机 一个完全无法被外界影响的状态机 构造函数, 传入一个函数, 两个参数, 分别是re ...
- 13.调整数组顺序使奇数位于偶数前面 Java
思路 两次遍历,第一次把奇数加到list中,第二次把偶数加到list中,时间复杂度为O(n). 利用类似于插入排序的方法,但时间复杂度为O(n^2). 代码 import java.util.Arra ...
- MySQL-插入更新 ON DUPLICATE KEY UPDATE
向数据库中插入一条记录,若该数据的主键值(UNIQUE KEY)已经在表中存在,则执行后面的 UPDATE 操作.否则执行前面的 INSERT 操作. 测试表结构 CREATE TABLE `flum ...