当前位置:网站首页>The number of consecutive subarrays whose leetcode/ product is less than k
The number of consecutive subarrays whose leetcode/ product is less than k
2022-07-29 02:03:00 【xcrj】
Code
package com.xcrj;
/** * The finger of the sword Offer II 009. The product is less than K Subarray * Given an array of positive integers nums And integer k , Please find out that the product in the array is less than k The number of successive subarrays of . * ( The array element product is less than k&& Array elements are continuous ) How many such arrays are there */
public class Solution9 {
/** * The sliding window * !!! Use different pointers to achieve different purposes * i j It all points to 0 * j Move back until product>=s * i Move back until product<s * sum+=j-i+1 */
public int numSubarrayProductLessThanK(int[] nums, int k) {
int i = 0;
int j = 0;
int product = 1;
int sum = 0;
while (j < nums.length) {
product *= nums[j];
// Input [1,2,3] 0 when ,product Always for 0,0/ Any number =0
// i<j avoid i Keep adding ,i==j The length of the subarray is 1 Satisfy <k It's fine too
while (i <= j && product >= k) {
product /= nums[i];
i++;
}
sum += j - i + 1;
j++;
}
return sum;
}
public static void main(String[] args) {
Solution9 solution9 = new Solution9();
System.out.println(solution9.numSubarrayProductLessThanK(new int[]{
10, 5, 2, 6}, 100));
}
}
Reference resources
author :LeetCode-Solution
link :https://leetcode.cn/problems/ZVAVXX/solution/cheng-ji-xiao-yu-k-de-zi-shu-zu-by-leetc-xqx8/
source : Power button (LeetCode)
边栏推荐
- 【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
- Sword finger offer special assault edition day 13
- 规划数学期末模拟考试一
- [netding cup 2020 rosefinch group]nmap
- More interesting Title Dynamic Effect
- 【MySQL】sql给表起别名
- The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
- 【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
- 【流放之路-第八章】
- Know that Chuangyu is listed in many fields of ccsip 2022 panorama
猜你喜欢

Anaconda environment installation problem

Data platform data access practice

Where will Jinan win in hosting the first computing power conference?
![[the road of Exile - Chapter 5]](/img/ef/7ecc1cb4a95c613f7be91f7acc761c.png)
[the road of Exile - Chapter 5]

数学建模——公交调度优化

【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
![What is a proxy server? [2022 guide]](/img/d8/f9ff56608ab42df9127493bc038961.png)
What is a proxy server? [2022 guide]

Leetcode 112: path sum

Tda75610-i2c-determination of I2C address of analog power amplifier
![[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"
随机推荐
把逻辑做在Sigma-DSP中的优化实例-数据分配器
【流放之路-第六章】
Come on, handwritten RPC S2 serialization exploration
使用本地缓存+全局缓存实现小型系统用户权限管理
JVM learning minutes
leetcode/和大于等于target的连续最短子数组
Planning mathematics final exam simulation II
About df['a column name'] [serial number]
Analyze OP based on autoware_ global_ Planner global path planning module re planning
druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
leetcode/乘积小于K 的连续子数组的个数
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
数学建模——带相变材料的低温防护服御寒仿真模拟
[WesternCTF2018]shrine
The scientific research environment has a great impact on people
Random talk on distributed development
数学建模——仓内拣货优化问题
LM13丨形态量化-动量周期分析
Sword finger offer special assault edition day 13