forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
40 lines (36 loc) · 929 Bytes
/
Solution.ts
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
class BinaryIndexedTree {
private n: number;
private c: number[];
constructor(n: number) {
this.n = n;
this.c = Array(n + 1).fill(0);
}
update(x: number, v: number): void {
for (; x <= this.n; x += x & -x) {
this.c[x] += v;
}
}
query(x: number): number {
let s = 0;
for (; x > 0; x -= x & -x) {
s += this.c[x];
}
return s;
}
}
function subarraysWithMoreZerosThanOnes(nums: number[]): number {
const n: number = nums.length;
const base: number = n + 1;
const tree: BinaryIndexedTree = new BinaryIndexedTree(n + base);
tree.update(base, 1);
const mod: number = 1e9 + 7;
let ans: number = 0;
let s: number = 0;
for (const x of nums) {
s += x || -1;
ans += tree.query(s - 1 + base);
ans %= mod;
tree.update(s + base, 1);
}
return ans;
}