Skip to content

Commit 001dcc6

Browse files
authored
feat: add solutions to lc problem: No.2143 (doocs#1493)
No.2143.Choose Numbers From Two Arrays in Range
1 parent 9cf8488 commit 001dcc6

File tree

7 files changed

+430
-6
lines changed

7 files changed

+430
-6
lines changed

solution/2100-2199/2143.Choose Numbers From Two Arrays in Range/README.md

Lines changed: 153 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,179 @@
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:动态规划**
75+
76+
我们定义 $f[i][j]$ 表示以第 $i$ 个元素结尾,且从 $nums1$ 中选取的数字和与从 $nums2$ 中选取的数字和之差为 $j$ 的平衡区间的个数。由于差值可能为负数,因此,我们统一将 $j$ 加上 $s_2 = \sum_{k=0}^{n-1}nums2[k]$,这样就可以保证 $j$ 为非负整数。
77+
78+
考虑 $f[i][j]$,我们可以单独将第 $i$ 个元素视为一个区间,那么 $f[i][nums1[i] + s_2]$ 和 $f[i][-nums2[i] + s_2]$ 都会增加 $1$。此外,如果 $i \gt 0$,我们也可以将第 $i$ 个元素添加到前面的某个区间中,我们在 $[0, s_1 + s_2]$ 范围内枚举 $j$,如果 $j \geq a$,那么 $f[i][j]$ 会增加 $f[i - 1][j - a]$,如果 $j + b \leq s_1 + s_2$,那么 $f[i][j]$ 会增加 $f[i - 1][j + b]$。
79+
80+
答案为 $\sum_{i=0}^{n-1}f[i][s_2]$。
81+
82+
时间复杂度 $O(n \times M)$,空间复杂度 $O(n \times M)$。其中 $n$ 和 $M$ 分别为数组 $nums1$ 的长度以及数字和的最大值。
83+
7484
<!-- tabs:start -->
7585

7686
### **Python3**
7787

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

8090
```python
81-
91+
class Solution:
92+
def countSubranges(self, nums1: List[int], nums2: List[int]) -> int:
93+
n = len(nums1)
94+
s1, s2 = sum(nums1), sum(nums2)
95+
f = [[0] * (s1 + s2 + 1) for _ in range(n)]
96+
ans = 0
97+
mod = 10**9 + 7
98+
for i, (a, b) in enumerate(zip(nums1, nums2)):
99+
f[i][a + s2] += 1
100+
f[i][-b + s2] += 1
101+
if i:
102+
for j in range(s1 + s2 + 1):
103+
if j >= a:
104+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod
105+
if j + b < s1 + s2 + 1:
106+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod
107+
ans = (ans + f[i][s2]) % mod
108+
return ans
82109
```
83110

84111
### **Java**
85112

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

88115
```java
116+
class Solution {
117+
public int countSubranges(int[] nums1, int[] nums2) {
118+
int n = nums1.length;
119+
int s1 = Arrays.stream(nums1).sum();
120+
int s2 = Arrays.stream(nums2).sum();
121+
int[][] f = new int[n][s1 + s2 + 1];
122+
int ans = 0;
123+
final int mod = (int) 1e9 + 7;
124+
for (int i = 0; i < n; ++i) {
125+
int a = nums1[i], b = nums2[i];
126+
f[i][a + s2]++;
127+
f[i][-b + s2]++;
128+
if (i > 0) {
129+
for (int j = 0; j <= s1 + s2; ++j) {
130+
if (j >= a) {
131+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
132+
}
133+
if (j + b <= s1 + s2) {
134+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
135+
}
136+
}
137+
}
138+
ans = (ans + f[i][s2]) % mod;
139+
}
140+
return ans;
141+
}
142+
}
143+
```
89144

145+
### **C++**
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
int countSubranges(vector<int>& nums1, vector<int>& nums2) {
151+
int n = nums1.size();
152+
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
153+
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
154+
int f[n][s1 + s2 + 1];
155+
memset(f, 0, sizeof(f));
156+
int ans = 0;
157+
const int mod = 1e9 + 7;
158+
for (int i = 0; i < n; ++i) {
159+
int a = nums1[i], b = nums2[i];
160+
f[i][a + s2]++;
161+
f[i][-b + s2]++;
162+
if (i) {
163+
for (int j = 0; j <= s1 + s2; ++j) {
164+
if (j >= a) {
165+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
166+
}
167+
if (j + b <= s1 + s2) {
168+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
169+
}
170+
}
171+
}
172+
ans = (ans + f[i][s2]) % mod;
173+
}
174+
return ans;
175+
}
176+
};
90177
```
91178
92-
### **TypeScript**
179+
### **Go**
180+
181+
```go
182+
func countSubranges(nums1 []int, nums2 []int) (ans int) {
183+
n := len(nums1)
184+
s1, s2 := sum(nums1), sum(nums2)
185+
f := make([][]int, n)
186+
for i := range f {
187+
f[i] = make([]int, s1+s2+1)
188+
}
189+
const mod int = 1e9 + 7
190+
for i, a := range nums1 {
191+
b := nums2[i]
192+
f[i][a+s2]++
193+
f[i][-b+s2]++
194+
if i > 0 {
195+
for j := 0; j <= s1+s2; j++ {
196+
if j >= a {
197+
f[i][j] = (f[i][j] + f[i-1][j-a]) % mod
198+
}
199+
if j+b <= s1+s2 {
200+
f[i][j] = (f[i][j] + f[i-1][j+b]) % mod
201+
}
202+
}
203+
}
204+
ans = (ans + f[i][s2]) % mod
205+
}
206+
return
207+
}
208+
209+
func sum(nums []int) (ans int) {
210+
for _, x := range nums {
211+
ans += x
212+
}
213+
return
214+
}
215+
```
93216

94-
<!-- 这里可写当前语言的特殊实现逻辑 -->
217+
### **TypeScript**
95218

96219
```ts
97-
220+
function countSubranges(nums1: number[], nums2: number[]): number {
221+
const n = nums1.length;
222+
const s1 = nums1.reduce((a, b) => a + b, 0);
223+
const s2 = nums2.reduce((a, b) => a + b, 0);
224+
const f: number[][] = Array(n)
225+
.fill(0)
226+
.map(() => Array(s1 + s2 + 1).fill(0));
227+
const mod = 1e9 + 7;
228+
let ans = 0;
229+
for (let i = 0; i < n; ++i) {
230+
const [a, b] = [nums1[i], nums2[i]];
231+
f[i][a + s2]++;
232+
f[i][-b + s2]++;
233+
if (i) {
234+
for (let j = 0; j <= s1 + s2; ++j) {
235+
if (j >= a) {
236+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
237+
}
238+
if (j + b <= s1 + s2) {
239+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
240+
}
241+
}
242+
}
243+
ans = (ans + f[i][s2]) % mod;
244+
}
245+
return ans;
246+
}
98247
```
99248

100249
### **...**

solution/2100-2199/2143.Choose Numbers From Two Arrays in Range/README_EN.md

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,160 @@ In the second balanced range, we choose nums2[1] and in the third balanced range
7272
### **Python3**
7373

7474
```python
75-
75+
class Solution:
76+
def countSubranges(self, nums1: List[int], nums2: List[int]) -> int:
77+
n = len(nums1)
78+
s1, s2 = sum(nums1), sum(nums2)
79+
f = [[0] * (s1 + s2 + 1) for _ in range(n)]
80+
ans = 0
81+
mod = 10**9 + 7
82+
for i, (a, b) in enumerate(zip(nums1, nums2)):
83+
f[i][a + s2] += 1
84+
f[i][-b + s2] += 1
85+
if i:
86+
for j in range(s1 + s2 + 1):
87+
if j >= a:
88+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod
89+
if j + b < s1 + s2 + 1:
90+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod
91+
ans = (ans + f[i][s2]) % mod
92+
return ans
7693
```
7794

7895
### **Java**
7996

8097
```java
98+
class Solution {
99+
public int countSubranges(int[] nums1, int[] nums2) {
100+
int n = nums1.length;
101+
int s1 = Arrays.stream(nums1).sum();
102+
int s2 = Arrays.stream(nums2).sum();
103+
int[][] f = new int[n][s1 + s2 + 1];
104+
int ans = 0;
105+
final int mod = (int) 1e9 + 7;
106+
for (int i = 0; i < n; ++i) {
107+
int a = nums1[i], b = nums2[i];
108+
f[i][a + s2]++;
109+
f[i][-b + s2]++;
110+
if (i > 0) {
111+
for (int j = 0; j <= s1 + s2; ++j) {
112+
if (j >= a) {
113+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
114+
}
115+
if (j + b <= s1 + s2) {
116+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
117+
}
118+
}
119+
}
120+
ans = (ans + f[i][s2]) % mod;
121+
}
122+
return ans;
123+
}
124+
}
125+
```
81126

127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
int countSubranges(vector<int>& nums1, vector<int>& nums2) {
133+
int n = nums1.size();
134+
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
135+
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
136+
int f[n][s1 + s2 + 1];
137+
memset(f, 0, sizeof(f));
138+
int ans = 0;
139+
const int mod = 1e9 + 7;
140+
for (int i = 0; i < n; ++i) {
141+
int a = nums1[i], b = nums2[i];
142+
f[i][a + s2]++;
143+
f[i][-b + s2]++;
144+
if (i) {
145+
for (int j = 0; j <= s1 + s2; ++j) {
146+
if (j >= a) {
147+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
148+
}
149+
if (j + b <= s1 + s2) {
150+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
151+
}
152+
}
153+
}
154+
ans = (ans + f[i][s2]) % mod;
155+
}
156+
return ans;
157+
}
158+
};
159+
```
160+
161+
### **Go**
162+
163+
```go
164+
func countSubranges(nums1 []int, nums2 []int) (ans int) {
165+
n := len(nums1)
166+
s1, s2 := sum(nums1), sum(nums2)
167+
f := make([][]int, n)
168+
for i := range f {
169+
f[i] = make([]int, s1+s2+1)
170+
}
171+
const mod int = 1e9 + 7
172+
for i, a := range nums1 {
173+
b := nums2[i]
174+
f[i][a+s2]++
175+
f[i][-b+s2]++
176+
if i > 0 {
177+
for j := 0; j <= s1+s2; j++ {
178+
if j >= a {
179+
f[i][j] = (f[i][j] + f[i-1][j-a]) % mod
180+
}
181+
if j+b <= s1+s2 {
182+
f[i][j] = (f[i][j] + f[i-1][j+b]) % mod
183+
}
184+
}
185+
}
186+
ans = (ans + f[i][s2]) % mod
187+
}
188+
return
189+
}
190+
191+
func sum(nums []int) (ans int) {
192+
for _, x := range nums {
193+
ans += x
194+
}
195+
return
196+
}
82197
```
83198

84199
### **TypeScript**
85200

86201
```ts
87-
202+
function countSubranges(nums1: number[], nums2: number[]): number {
203+
const n = nums1.length;
204+
const s1 = nums1.reduce((a, b) => a + b, 0);
205+
const s2 = nums2.reduce((a, b) => a + b, 0);
206+
const f: number[][] = Array(n)
207+
.fill(0)
208+
.map(() => Array(s1 + s2 + 1).fill(0));
209+
const mod = 1e9 + 7;
210+
let ans = 0;
211+
for (let i = 0; i < n; ++i) {
212+
const [a, b] = [nums1[i], nums2[i]];
213+
f[i][a + s2]++;
214+
f[i][-b + s2]++;
215+
if (i) {
216+
for (let j = 0; j <= s1 + s2; ++j) {
217+
if (j >= a) {
218+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
219+
}
220+
if (j + b <= s1 + s2) {
221+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
222+
}
223+
}
224+
}
225+
ans = (ans + f[i][s2]) % mod;
226+
}
227+
return ans;
228+
}
88229
```
89230

90231
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int countSubranges(vector<int>& nums1, vector<int>& nums2) {
4+
int n = nums1.size();
5+
int s1 = accumulate(nums1.begin(), nums1.end(), 0);
6+
int s2 = accumulate(nums2.begin(), nums2.end(), 0);
7+
int f[n][s1 + s2 + 1];
8+
memset(f, 0, sizeof(f));
9+
int ans = 0;
10+
const int mod = 1e9 + 7;
11+
for (int i = 0; i < n; ++i) {
12+
int a = nums1[i], b = nums2[i];
13+
f[i][a + s2]++;
14+
f[i][-b + s2]++;
15+
if (i) {
16+
for (int j = 0; j <= s1 + s2; ++j) {
17+
if (j >= a) {
18+
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod;
19+
}
20+
if (j + b <= s1 + s2) {
21+
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod;
22+
}
23+
}
24+
}
25+
ans = (ans + f[i][s2]) % mod;
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)