当前位置:网站首页>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)
边栏推荐
- Sword finger offer special assault edition day 13
- Force deduction brush question (2): sum of three numbers
- JS timer setinterval clearinterval delayer setTimeout asynchronous animation
- Minimalist thrift+consumer
- [the road of Exile - Chapter 5]
- Planning mathematics final simulation exam I
- The solution of reducing the sharpness of pictures after inserting into word documents
- LeetCode 113:路径总和 II
- Day01 job
- Data platform data access practice
猜你喜欢

With the explosive growth of digital identity in 2022, global organizations are facing greater network security

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?

LeetCode 112:路径总和

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

Minimalist thrift+consumer

为什么 BI 软件都搞不定关联分析

druid. IO custom real-time task scheduling policy

The brutal rule of blackmail software continues, and attacks increase by 105%

MySQL high performance optimization notes (including 578 pages of notes PDF document), collected

【流放之路-第七章】
随机推荐
[the road of Exile - Chapter 5]
JVM learning minutes
How to crawl web pages with playwright?
[the road of Exile - Chapter 8]
【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
数学建模——永冻土层上关于路基热传导问题
leetcode/和为k的连续子数组个数
Large scale web crawling of e-commerce websites (Ultimate Guide)
Leetcode 113: path sum II
Sigma-DSP-OUTPUT
FPGA实现10M多功能信号发生器
What is the ISO assessment? How to do the waiting insurance scheme
【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】
What are the common cyber threats faced by manufacturers and how do they protect themselves
Reinforcement learning (III): dqn, nature dqn, double dqn, with source code interpretation
分布式开发漫谈
Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
What is browser fingerprint recognition
Data security is a competitive advantage. How can companies give priority to information security and compliance
[网鼎杯 2020 朱雀组]Nmap