forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (27 loc) · 888 Bytes
/
Solution.java
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
class Solution {
public int countSubranges(int[] nums1, int[] nums2) {
int n = nums1.length;
int s1 = Arrays.stream(nums1).sum();
int s2 = Arrays.stream(nums2).sum();
int[][] f = new int[n][s1 + s2 + 1];
int ans = 0;
final int mod = (int) 1e9 + 7;
for (int i = 0; i < n; ++i) {
int a = nums1[i], b = nums2[i];
f[i][a + s2]++;
f[i][-b + s2]++;
if (i > 0) {
for (int j = 0; j <= s1 + s2; ++j) {
if (j >= a) {
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
}
if (j + b <= s1 + s2) {
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
}
}
}
ans = (ans + f[i][s2]) % mod;
}
return ans;
}
}