Skip to content

Latest commit

 

History

History
77 lines (63 loc) · 1.65 KB

File metadata and controls

77 lines (63 loc) · 1.65 KB

1248. 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

Solutions (Ruby)

1. HashMap

# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def number_of_subarrays(nums, k)
  counter = { 0 => 1 }
  counter.default = 0
  count = 0
  ret = 0

  nums.each do |x|
    count += 1 if x.odd?
    ret += counter[count - k]
    counter[count] += 1
  end

  ret
end

Solutions (Rust)

1. HashMap

use std::collections::HashMap;

impl Solution {
    pub fn number_of_subarrays(nums: Vec<i32>, k: i32) -> i32 {
        let mut counter = vec![(0, 1)].into_iter().collect::<HashMap<_, _>>();
        let mut count = 0;
        let mut ret = 0;

        for i in 0..nums.len() {
            if nums[i] % 2 == 1 {
                count += 1;
            }
            ret += counter.get(&(count - k)).unwrap_or(&0);
            *counter.entry(count).or_insert(0) += 1;
        }

        ret
    }
}