js-practice/src/array/count-number-of-nice-subarrays.js

53 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 参考https://leetcode-cn.com/problems/count-number-of-nice-subarrays/solution/count-number-of-nice-subarrays-by-ikaruga/
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
export const numberOfSubarrays = function (nums, k) {
const odd = []
odd.push(-1)
let ans = 0
let i = 1
for (let j = 0; j <= nums.length; j++) {
if (j === nums.length || (nums[j] & 1)) {
odd.push(j)
}
if (odd.length - i > k) {
const left = odd[i] - odd[i - 1]
const right = j - odd[odd.length - 2]
ans += left * right
i++
}
}
return ans
}
/**
* 错误解法
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
// export const numberOfSubarrays = function (nums, k) {
// let res = 0
// for (let n = 0, len = nums.length; n < len; n++) {
// let num = 0
// let i = n
// while (num < k && i < len) {
// if (nums[i] % 2 !== 0) {
// num++
// }
// i++
// }
// if (num === k) {
// res++
// }
// }
// return res
// }