-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1248-CountNumberOfNiceSubarrays.go
More file actions
109 lines (96 loc) · 2.96 KB
/
1248-CountNumberOfNiceSubarrays.go
File metadata and controls
109 lines (96 loc) · 2.96 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
// 1248. Count Number of Nice Subarrays
// Given an array of integers nums and an integer k.
// A continuous 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 are 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
import "fmt"
func numberOfSubarrays(nums []int, k int) int {
prefixSums := map[int]int{0: 1}
sum, res := 0, 0
for _, v := range nums {
sum += v % 2 // 统计奇数
res += prefixSums[sum - k]
prefixSums[sum]++
}
// fmt.Println(prefixSums)
return res
}
func numberOfSubarrays1(nums []int, k int) int {
for i, x := range nums {
nums[i] = x&1
}
res, left1, left2, sum1, sum2 := 0, 0, 0, 0, 0
for right, x := range nums {
sum1 += x
sum2 += x
for left1 <= right && sum1 > k {
sum1 -= nums[left1]
left1++
}
for left2 <= right && sum2 >= k {
sum2 -= nums[left2]
left2++
}
res += left2-left1
}
return res
}
func numberOfSubarrays2(nums []int, k int) int {
res, q, k_in_q, lr := 0, []int{}, 0, []int{0}
for _, v := range nums {
if v%2 != 0 && k_in_q == k {
res += (lr[0]+1) * (lr[len(lr)-1]+1)
q = append(q[lr[0]+1:], v)
lr = lr[1:]
lr = append(lr, 0)
} else if v%2 != 0 {
k_in_q++
q = append(q, v)
lr = append(lr, 0)
} else {
q = append(q, v)
lr[len(lr)-1]++
}
}
if len(lr) > k {
res += (lr[0]+1) * (lr[len(lr)-1]+1)
}
return res
}
func main() {
// 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].
fmt.Println(numberOfSubarrays([]int{1,1,2,1,1}, 3)) // 2
// Example 2:
// Input: nums = [2,4,6], k = 1
// Output: 0
// Explanation: There are no odd numbers in the array.
fmt.Println(numberOfSubarrays([]int{2,4,6}, 1)) // 0
// Example 3:
// Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
// Output: 16
fmt.Println(numberOfSubarrays([]int{2,2,2,1,2,2,1,2,2,2}, 2)) // 16
fmt.Println(numberOfSubarrays1([]int{1,1,2,1,1}, 3)) // 2
fmt.Println(numberOfSubarrays1([]int{2,4,6}, 1)) // 0
fmt.Println(numberOfSubarrays1([]int{2,2,2,1,2,2,1,2,2,2}, 2)) // 16
fmt.Println(numberOfSubarrays2([]int{1,1,2,1,1}, 3)) // 2
fmt.Println(numberOfSubarrays2([]int{2,4,6}, 1)) // 0
fmt.Println(numberOfSubarrays2([]int{2,2,2,1,2,2,1,2,2,2}, 2)) // 16
}