当前位置:网站首页>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)
边栏推荐
- Practical experience of Google cloud spanner
- As long as I run fast enough, it won't catch me. How does a high school student achieve a 70% salary increase under the epidemic?
- 【golang】使用select {}
- Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
- Minimalist thrift+consumer
- 使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包
- Data security is a competitive advantage. How can companies give priority to information security and compliance
- 【流放之路-第八章】
- (CVPR-2019)选择性的内核网络
- Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
猜你喜欢

golang启动报错【已解决】
![[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"

FPGA实现10M多功能信号发生器

The basic concept of transaction and the implementation principle of MySQL transaction

Planning mathematics final simulation exam I

Leetcode 112: path sum
![[golang] use select {}](/img/30/fa593ec682a40c47689c1fd88f9b83.png)
[golang] use select {}

Minimalist thrift+consumer
![[UE4] replay game playback for ue4.26](/img/c3/1c7b30797f46dbd323cac4d158600f.png)
[UE4] replay game playback for ue4.26

LM13丨形态量化-动量周期分析
随机推荐
leetcode/和大于等于target的连续最短子数组
规划数学期末考试模拟二
[7.21-26] code source - [good sequence] [social circle] [namonamo]
About df['a column name'] [serial number]
Comprehensive explanation of "search engine crawl"
How to protect WordPress website from network attack? It is essential to take safety measures
Yocto project download and compilation
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
Analyze OP based on autoware_ global_ Planner global path planning module re planning
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
Lua log implementation -- print table
[UE4] replay game playback for ue4.26
leetcode/和为k的连续子数组个数
[the road of Exile - Chapter 7]
Internship: tool class writing for type judgment
MySQL high performance optimization notes (including 578 pages of notes PDF document), collected
LeetCode 113:路径总和 II
Sword finger offer special assault edition day 13
With the explosive growth of digital identity in 2022, global organizations are facing greater network security
使用本地缓存+全局缓存实现小型系统用户权限管理