当前位置:网站首页>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)
边栏推荐
- More interesting Title Dynamic Effect
- JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
- FPGA实现10M多功能信号发生器
- 九天后我们一起,聚焦音视频、探秘技术新发展
- What is the ISO assessment? How to do the waiting insurance scheme
- Sigma-DSP-OUTPUT
- 给LaTeX公式添加优美的注解;日更『数据科学』面试题集锦;大学生『计算机』自学指南;个人防火墙;前沿资料/论文 | ShowMeAI资讯日报
- Js DOM2 和 DOM3
- Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
- Slow storage scheme
猜你喜欢
![[the road of Exile - Chapter 2]](/img/98/0a0558dc385141dbb4f97bc0e68b70.png)
[the road of Exile - Chapter 2]

ciscn 2022 华中赛区 misc

E-commerce keyword research helps data collection

【流放之路-第五章】

给LaTeX公式添加优美的注解;日更『数据科学』面试题集锦;大学生『计算机』自学指南;个人防火墙;前沿资料/论文 | ShowMeAI资讯日报

What is browser fingerprint recognition

DSP vibration seat

动态内存与智能指针

知道创宇上榜CCSIP 2022全景图多个领域

Process -- user address space and kernel address space
随机推荐
What are the common cyber threats faced by manufacturers and how do they protect themselves
知道创宇上榜CCSIP 2022全景图多个领域
LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
In depth analysis of C language memory alignment
About df['a column name'] [serial number]
Data platform data access practice
【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
(arxiv-2018) 重新审视基于视频的 Person ReID 的时间建模
Monadic linear function perceptron: Rosenblatt perceptron
Come on, handwritten RPC S2 serialization exploration
Comprehensive analysis of news capture doorway
【流放之路-第六章】
【golang】使用select {}
ciscn 2022 华中赛区 misc
Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?
StoneDB 邀请您参与开源社区月会!
使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包
Some summaries of ibatis script and provider
Force deduction brush question (2): sum of three numbers
把逻辑做在Sigma-DSP中的优化实例-数据分配器