Skip to content

Commit f383db7

Browse files
author
Hieu Luong
committed
Update Problem 1248 - Count Number of nice subarrays
1 parent 5e97f89 commit f383db7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/slidingwindow/Problem1248_CountNumberOfNiceSubarrays.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ public int numberOfSubarrays_DP(int[] A, int k) {
5454
}
5555
return res;
5656
}
57+
58+
public int numberOfSubarrays_shortVersion(int[] A, int k) {
59+
60+
// (l+1)*(r+1)
61+
int res = 0, l = 0, count = 0;
62+
for (int i = 0, j = 0; i < A.length; i++) {
63+
count += A[i]%2;
64+
if (count == k) l = 0;
65+
while (count == k) {
66+
count -= A[j++]%2;
67+
l++;
68+
}
69+
res += l;
70+
}
71+
return res;
72+
}
5773
}

0 commit comments

Comments
 (0)