Skip to content

Commit 9347515

Browse files
committed
feat: add solutions to lc problem: No.2031
No.2031.Count Subarrays With More Ones Than Zeros
1 parent 14b7bbf commit 9347515

File tree

14 files changed

+644
-33
lines changed

14 files changed

+644
-33
lines changed

solution/2000-2099/2031.Count Subarrays With More Ones Than Zeros/README.md

+212
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,234 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:树状数组**
57+
58+
树状数组。
59+
60+
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
61+
62+
1. **单点更新** `update(x, delta)`: 把序列 x 位置的数加上一个值 delta;
63+
1. **前缀和查询** `query(x)`:查询序列 `[1,...x]` 区间的区间和,即位置 x 的前缀和。
64+
65+
这两个操作的时间复杂度均为 `O(log n)`
66+
5667
<!-- tabs:start -->
5768

5869
### **Python3**
5970

6071
<!-- 这里可写当前语言的特殊实现逻辑 -->
6172

6273
```python
74+
class BinaryIndexedTree:
75+
def __init__(self, n):
76+
n += int(1e5 + 1)
77+
self.n = n
78+
self.c = [0] * (n + 1)
79+
80+
@staticmethod
81+
def lowbit(x):
82+
x += int(1e5 + 1)
83+
return x & -x
84+
85+
def update(self, x, delta):
86+
x += int(1e5 + 1)
87+
while x <= self.n:
88+
self.c[x] += delta
89+
x += BinaryIndexedTree.lowbit(x)
90+
91+
def query(self, x):
92+
x += int(1e5 + 1)
93+
s = 0
94+
while x > 0:
95+
s += self.c[x]
96+
x -= BinaryIndexedTree.lowbit(x)
97+
return s
6398

99+
100+
class Solution:
101+
def subarraysWithMoreZerosThanOnes(self, nums: List[int]) -> int:
102+
n = len(nums)
103+
s = [0]
104+
for v in nums:
105+
s.append(s[-1] + (v or -1))
106+
tree = BinaryIndexedTree(n + 1)
107+
MOD = int(1e9 + 7)
108+
ans = 0
109+
for v in s:
110+
ans = (ans + tree.query(v - 1)) % MOD
111+
tree.update(v, 1)
112+
return ans
64113
```
65114

66115
### **Java**
67116

68117
<!-- 这里可写当前语言的特殊实现逻辑 -->
69118

70119
```java
120+
class BinaryIndexedTree {
121+
private int n;
122+
private int[] c;
123+
124+
public BinaryIndexedTree(int n) {
125+
n += (int) 1e5 + 1;
126+
this.n = n;
127+
c = new int[n + 1];
128+
}
129+
130+
public void update(int x, int delta) {
131+
x += (int) 1e5 + 1;
132+
while (x <= n) {
133+
c[x] += delta;
134+
x += lowbit(x);
135+
}
136+
}
137+
138+
public int query(int x) {
139+
x += (int) 1e5 + 1;
140+
int s = 0;
141+
while (x > 0) {
142+
s += c[x];
143+
x -= lowbit(x);
144+
}
145+
return s;
146+
}
147+
148+
public static int lowbit(int x) {
149+
x += (int) 1e5 + 1;
150+
return x & -x;
151+
}
152+
}
153+
154+
class Solution {
155+
private static final int MOD = (int) 1e9 + 7;
156+
157+
public int subarraysWithMoreZerosThanOnes(int[] nums) {
158+
int n = nums.length;
159+
int[] s = new int[n + 1];
160+
for (int i = 0; i < n; ++i) {
161+
s[i + 1] = s[i] + (nums[i] == 1 ? 1 : -1);
162+
}
163+
BinaryIndexedTree tree = new BinaryIndexedTree(n + 1);
164+
int ans = 0;
165+
for (int v : s) {
166+
ans = (ans + tree.query(v - 1)) % MOD;
167+
tree.update(v, 1);
168+
}
169+
return ans;
170+
}
171+
}
172+
```
173+
174+
### **C++**
175+
176+
```cpp
177+
class BinaryIndexedTree {
178+
public:
179+
int n;
180+
vector<int> c;
181+
182+
BinaryIndexedTree(int _n): n(_n + 1e5 + 1), c(_n + 1 + 1e5 + 1){}
183+
184+
void update(int x, int delta) {
185+
x += 1e5 + 1;
186+
while (x <= n)
187+
{
188+
c[x] += delta;
189+
x += lowbit(x);
190+
}
191+
}
192+
193+
int query(int x) {
194+
x += 1e5 + 1;
195+
int s = 0;
196+
while (x > 0)
197+
{
198+
s += c[x];
199+
x -= lowbit(x);
200+
}
201+
return s;
202+
}
203+
204+
int lowbit(int x) {
205+
x += 1e5 + 1;
206+
return x & -x;
207+
}
208+
};
209+
210+
class Solution {
211+
public:
212+
int subarraysWithMoreZerosThanOnes(vector<int>& nums) {
213+
int n = nums.size();
214+
vector<int> s(n + 1);
215+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + (nums[i] == 1 ? 1 : -1);
216+
BinaryIndexedTree* tree = new BinaryIndexedTree(n + 1);
217+
int ans = 0;
218+
const int MOD = 1e9 + 7;
219+
for (int v : s)
220+
{
221+
ans = (ans + tree->query(v - 1)) % MOD;
222+
tree->update(v, 1);
223+
}
224+
return ans;
225+
}
226+
};
227+
```
228+
229+
### **Go**
230+
231+
```go
232+
type BinaryIndexedTree struct {
233+
n int
234+
c []int
235+
}
236+
237+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
238+
n += 1e5 + 1
239+
c := make([]int, n+1)
240+
return &BinaryIndexedTree{n, c}
241+
}
242+
243+
func (this *BinaryIndexedTree) lowbit(x int) int {
244+
x += 1e5 + 1
245+
return x & -x
246+
}
247+
248+
func (this *BinaryIndexedTree) update(x, delta int) {
249+
x += 1e5 + 1
250+
for x <= this.n {
251+
this.c[x] += delta
252+
x += this.lowbit(x)
253+
}
254+
}
255+
256+
func (this *BinaryIndexedTree) query(x int) int {
257+
s := 0
258+
x += 1e5 + 1
259+
for x > 0 {
260+
s += this.c[x]
261+
x -= this.lowbit(x)
262+
}
263+
return s
264+
}
71265
266+
func subarraysWithMoreZerosThanOnes(nums []int) int {
267+
n := len(nums)
268+
s := make([]int, n+1)
269+
for i, v := range nums {
270+
if v == 0 {
271+
v = -1
272+
}
273+
s[i+1] = s[i] + v
274+
}
275+
tree := newBinaryIndexedTree(n + 1)
276+
ans := 0
277+
mod := int(1e9 + 7)
278+
for _, v := range s {
279+
ans = (ans + tree.query(v-1)) % mod
280+
tree.update(v, 1)
281+
}
282+
return ans
283+
}
72284
```
73285

74286
### **...**

0 commit comments

Comments
 (0)