-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1248.count-number-of-nice-subarrays.java
90 lines (78 loc) · 1.79 KB
/
1248.count-number-of-nice-subarrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* @lc app=leetcode id=1248 lang=java
*
* [1248] Count Number of Nice Subarrays
*
* https://leetcode.com/problems/count-number-of-nice-subarrays/description/
*
* algorithms
* Medium (54.66%)
* Likes: 296
* Dislikes: 9
* Total Accepted: 12.1K
* Total Submissions: 22K
* Testcase Example: '[1,1,2,1,1]\n3'
*
* Given an array of integers nums and an integer k. A 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
*
*
*/
// @lc code=start
// 1248. Count Number of Nice Subarrays
// when moving right pointer, if there are matching before, add them into ans
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
if (nums.length == 0 || k < 0) return 0;
int left = 0;
int match = 0; // record how many matches from moving left pointer
int oddCount = 0;
int ans = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] % 2 == 1) oddCount++;
if (oddCount == k) match = 0;
while (oddCount == k) {
match++;
if (nums[left++] % 2 == 1) oddCount--;
}
ans += match;
}
return ans;
}
}
// @lc code=end