给你一个只包含 0
和 1
的数组 nums
,请返回 1
的数量 大于 0
的数量的子数组的个数。由于答案可能很大,请返回答案对 109 + 7
取余 的结果。
一个 子数组 指的是原数组中连续的一个子序列。
示例 1:
输入: nums = [0,1,1,0,1] 输出: 9 解释: 长度为 1 的、1 的数量大于 0 的数量的子数组有: [1], [1], [1] 长度为 2 的、1 的数量大于 0 的数量的子数组有: [1,1] 长度为 3 的、1 的数量大于 0 的数量的子数组有: [0,1,1], [1,1,0], [1,0,1] 长度为 4 的、1 的数量大于 0 的数量的子数组有: [1,1,0,1] 长度为 5 的、1 的数量大于 0 的数量的子数组有: [0,1,1,0,1]
示例 2:
输入: nums = [0] 输出: 0 解释: 没有子数组的 1 的数量大于 0 的数量。
示例 3:
输入: nums = [1] 输出: 1 解释: 长度为 1 的、1 的数量大于 0 的数量的子数组有: [1]
提示:
1 <= nums.length <= 105
0 <= nums[i] <= 1
题目需要我们统计所有子数组中
求子数组的元素和,可以使用前缀和来实现。为了统计所有子数组中元素和大于
接下来,我们遍历数组
最后返回
时间复杂度
class BinaryIndexedTree:
__slots__ = ["n", "c"]
def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)
def update(self, x: int, v: int):
while x <= self.n:
self.c[x] += v
x += x & -x
def query(self, x: int) -> int:
s = 0
while x:
s += self.c[x]
x -= x & -x
return s
class Solution:
def subarraysWithMoreZerosThanOnes(self, nums: List[int]) -> int:
n = len(nums)
base = n + 1
tree = BinaryIndexedTree(n + base)
tree.update(base, 1)
mod = 10**9 + 7
ans = s = 0
for x in nums:
s += x or -1
ans += tree.query(s - 1 + base)
ans %= mod
tree.update(s + base, 1)
return ans
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void update(int x, int v) {
for (; x <= n; x += x & -x) {
c[x] += v;
}
}
public int query(int x) {
int s = 0;
for (; x > 0; x -= x & -x) {
s += c[x];
}
return s;
}
}
class Solution {
public int subarraysWithMoreZerosThanOnes(int[] nums) {
int n = nums.length;
int base = n + 1;
BinaryIndexedTree tree = new BinaryIndexedTree(n + base);
tree.update(base, 1);
final int mod = (int) 1e9 + 7;
int ans = 0, s = 0;
for (int x : nums) {
s += x == 0 ? -1 : 1;
ans += tree.query(s - 1 + base);
ans %= mod;
tree.update(s + base, 1);
}
return ans;
}
}
class BinaryIndexedTree {
private:
int n;
vector<int> c;
public:
BinaryIndexedTree(int n)
: n(n)
, c(n + 1, 0) {}
void update(int x, int v) {
for (; x <= n; x += x & -x) {
c[x] += v;
}
}
int query(int x) {
int s = 0;
for (; x > 0; x -= x & -x) {
s += c[x];
}
return s;
}
};
class Solution {
public:
int subarraysWithMoreZerosThanOnes(vector<int>& nums) {
int n = nums.size();
int base = n + 1;
BinaryIndexedTree tree(n + base);
tree.update(base, 1);
const int mod = 1e9 + 7;
int ans = 0, s = 0;
for (int x : nums) {
s += (x == 0) ? -1 : 1;
ans += tree.query(s - 1 + base);
ans %= mod;
tree.update(s + base, 1);
}
return ans;
}
};
type BinaryIndexedTree struct {
n int
c []int
}
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
return &BinaryIndexedTree{n: n, c: make([]int, n+1)}
}
func (bit *BinaryIndexedTree) update(x, v int) {
for ; x <= bit.n; x += x & -x {
bit.c[x] += v
}
}
func (bit *BinaryIndexedTree) query(x int) (s int) {
for ; x > 0; x -= x & -x {
s += bit.c[x]
}
return
}
func subarraysWithMoreZerosThanOnes(nums []int) (ans int) {
n := len(nums)
base := n + 1
tree := newBinaryIndexedTree(n + base)
tree.update(base, 1)
const mod = int(1e9) + 7
s := 0
for _, x := range nums {
if x == 0 {
s--
} else {
s++
}
ans += tree.query(s - 1 + base)
ans %= mod
tree.update(s+base, 1)
}
return
}
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;
}
from sortedcontainers import SortedList
class Solution:
def subarraysWithMoreZerosThanOnes(self, nums: List[int]) -> int:
sl = SortedList([0])
mod = 10**9 + 7
ans = s = 0
for x in nums:
s += x or -1
ans += sl.bisect_left(s)
ans %= mod
sl.add(s)
return ans