Skip to content

Commit 2bd146a

Browse files
committed
Nice SubArray
1 parent 3643bc1 commit 2bd146a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

June22/NiceSubarray.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class NiceSubarray{
2+
public int numberOfSubarrays(int[] nums, int k) {
3+
int n = nums.length;
4+
int[] cnt = new int[n + 1];
5+
cnt[0] = 1;
6+
int ans = 0, t = 0;
7+
for (int v : nums) {
8+
t += v & 1;
9+
if (t - k >= 0) {
10+
ans += cnt[t - k];
11+
}
12+
cnt[t]++;
13+
}
14+
return ans;
15+
}
16+
}

0 commit comments

Comments
 (0)