首页 > 其他分享 >LeetCode 1248. Count Number of Nice Subarrays

LeetCode 1248. Count Number of Nice Subarrays

时间:2022-10-29 13:22:06浏览次数:96  
标签:Count count nums int Subarrays Number runner walker odd

原题链接在这里:https://leetcode.com/problems/count-number-of-nice-subarrays/

题目:

Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.

Return the number of nice sub-arrays.

Example 1:

Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].

Example 2:

Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.

Example 3:

Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16

Constraints:

  • 1 <= nums.length <= 50000
  • 1 <= nums[i] <= 10^5
  • 1 <= k <= nums.length

题解:

When runner hits a odd, k--.

While k == 0, move the walker. And count how many steps walker could move it means how many extended even numbers there could be.

e.g. 2, 2, 2, 1, 2, 2, 1, 2, 2. walker could move form 0 to 3 totally 4 steps, which corresponds to subarray with 0 count of 2, 1 count of 2, 2 count of 2 and 3 count of 2. 

Then where ever runner moves one, there could be 4 possible subarrays from the left side.

Time Complexity: O(n). n = nums.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int numberOfSubarrays(int[] nums, int k) {
 3         int n = nums.length;
 4         int res = 0;
 5         int walker = 0;
 6         int runner = 0;
 7         int count = 0;
 8         while(runner < n){
 9             if(nums[runner++] % 2 == 1){
10                 k--;
11                 count = 0;
12             }
13             
14             while(k == 0){
15                 k += nums[walker++] % 2;
16                 count++;
17             }
18             
19             res += count;
20         }
21         
22         return res;
23     }
24 }

类似Count Subarrays With Fixed Bounds.

标签:Count,count,nums,int,Subarrays,Number,runner,walker,odd
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16838547.html

相关文章

  • 【POJ1430】Binary Stirling Numbers(第二类斯特林数,组合数)
    求\(\begin{Bmatrix}n\\m\end{Bmatrix}\bmod2\)的值。由第二类斯特林数的递推公式:\[\begin{Bmatrix}n\\m\end{Bmatrix}=\begin{Bmatrix}n-1\\m-1\end{Bmatrix}+m\begi......
  • 【AGC005D】_K Perm Counting(容斥,二分图,计数dp)
    首先正面做不太好做,考虑容斥。设\(f(m)\)表示排列中至少有\(m\)处\(|P_i-i|=k\)的方案数。那么答案就是\(\sum\limits_{i=0}^n(-1)^if(i)\)。原题可以看成一个二......
  • Leetcode第907题:子数组的最小值之和(Sum of subarrays minimums )
    解题思路既然我们不能先遍历区间,然后找最小值,那么我们不如顺序倒过来,对于每个值,我们找有多少区间里面,它是最小值。对于一个数字A[i]来说,如果在某个区间[j,k]里面它......
  • 8.CF446C DZY Loves Fibonacci Numbers 线段树Lazy标记
    8.CF446CDZYLovesFibonacciNumbers线段树Lazy标记给定序列,要求支持区间对应项加斐波那契数列,区间求和洛谷传送门:​​CF446CDZYLovesFibonacciNumbers-洛谷|计......
  • 利用OFFSET函数与COUNTA函数创建动态名称,数据动态变化
    我们在excel中可以利用OFFSET函数与COUNTA函数的组合,可以创建一个动态的名称。动态名称是名称的高级用法,能够实现对一个未知大小的区域的引用,利用OFFSET函数与COUNTA函数创......
  • Codeforces Round #673 (Div. 2) C. k-Amazing Numbers
    题面Youaregivenanarrayaconsistingofnintegersnumberedfrom1ton.Let’sdefinethek-amazingnumberofthearrayastheminimumnumberthatoccurs......
  • PAT_甲级_1004 Counting Leaves (30分) (C++)
    目录​​1,题目描述​​​​题目大意​​​​输入:​​​​输出:​​​​2,解题思路​​​​关键:​​​​存储结构:​​​​dfs算法实现:​​​​3,代码【C++】​​1,题目描述 Samp......
  • Natural Number Game
    因为用Verilog会有颜色显示,所以用的Verilog。记录一下NaturalNumberGame的答案,好久以前做的,但没做完。目录TutorialworldAdditionworldMultiplicationworldFun......
  • How many prime numbers
    题目链接Howmanyprimenumbers大素数的判定解题思路miller_rabinmiller_rabin是一种概率性素数测试,原理基于费马素数测试,即如果\(n\)为素数,则在\(1\simn\)......
  • I Love Big Numbers !(高精度)
    题目链接题意:多组数据输入也就是C++中的:intn;while(cin>>n){代码块}对于每个数据输出其阶乘的各位上的数字之和。大眼一看,没有思路,那就百度把。百度解法......