Skip to content

Commit 432b241

Browse files
committed
update
1 parent 3cc8f8a commit 432b241

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

leetcode_solved/[editing]leetcode_1248_Count_Number_of_Nice_Subarrays.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// AC:: Runtime: 36 ms, faster than 26.88% of Java online submissions for Count Number of Nice Subarrays.
2+
// Memory Usage: 47 MB, less than 95.84% of Java online submissions for Count Number of Nice Subarrays.
3+
// same thought as prefixSum, see @ https://leetcode.com/problems/subarray-sum-equals-k/discuss/1380435/O(n2)-and-O(n)-or-Two-java-solutions-with-annotation
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public int numberOfSubarrays(int[] nums, int k) {
8+
int ret = 0, size = nums.length;
9+
int oddCountSum = 0;
10+
HashMap<Integer, Integer> record = new HashMap<>();
11+
record.put(0, 1);
12+
for (int num : nums) {
13+
if (num % 2 == 1) {
14+
oddCountSum++;
15+
}
16+
17+
if (record.get(oddCountSum - k) != null) {
18+
ret += record.get(oddCountSum - k);
19+
}
20+
21+
record.merge(oddCountSum, 1, Integer::sum);
22+
}
23+
24+
return ret;
25+
}
26+
}

0 commit comments

Comments
 (0)