Skip to content

Commit b9593eb

Browse files
committed
feat: add solutions to lc problem: No.0410.Split Array Largest Sum
1 parent 35ef049 commit b9593eb

File tree

6 files changed

+349
-21
lines changed

6 files changed

+349
-21
lines changed

solution/0400-0499/0410.Split Array Largest Sum/README.md

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,150 @@
4545
<li><code>1 <= m <= min(50, nums.length)</code></li>
4646
</ul>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
5251

52+
二分查找。
53+
54+
二分枚举**子数组的和的最大值**,找到满足条件的最小值。
55+
5356
<!-- tabs:start -->
5457

5558
### **Python3**
5659

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

5962
```python
60-
63+
class Solution:
64+
def splitArray(self, nums: List[int], m: int) -> int:
65+
def check(x):
66+
s, cnt = 0, 1
67+
for num in nums:
68+
if s + num > x:
69+
cnt += 1
70+
s = num
71+
else:
72+
s += num
73+
return cnt <= m
74+
75+
left, right = max(nums), sum(nums)
76+
while left < right:
77+
mid = (left + right) >> 1
78+
if check(mid):
79+
right = mid
80+
else:
81+
left = mid + 1
82+
return left
6183
```
6284

6385
### **Java**
6486

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

6789
```java
90+
class Solution {
91+
public int splitArray(int[] nums, int m) {
92+
int mx = -1;
93+
for (int num : nums) {
94+
mx = Math.max(mx, num);
95+
}
96+
int left = mx, right = (int) 1e9;
97+
while (left < right) {
98+
int mid = (left + right) >> 1;
99+
if (check(nums, m, mid)) {
100+
right = mid;
101+
} else {
102+
left = mid + 1;
103+
}
104+
}
105+
return left;
106+
}
107+
108+
private boolean check(int[] nums, int m, int x) {
109+
int s = 0, cnt = 1;
110+
for (int num : nums) {
111+
if (s + num > x) {
112+
++cnt;
113+
s = num;
114+
} else {
115+
s += num;
116+
}
117+
}
118+
return cnt <= m;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int splitArray(vector<int>& nums, int m) {
129+
int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9;
130+
while (left < right) {
131+
int mid = left + right >> 1;
132+
if (check(nums, m, mid)) right = mid;
133+
else left = mid + 1;
134+
}
135+
return left;
136+
}
137+
138+
bool check(vector<int>& nums, int m, int x) {
139+
int s = 0, cnt = 1;
140+
for (int num : nums) {
141+
if (s + num > x) {
142+
++cnt;
143+
s = num;
144+
} else {
145+
s += num;
146+
}
147+
}
148+
return cnt <= m;
149+
}
150+
};
151+
```
68152

153+
### **Go**
154+
155+
```go
156+
func splitArray(nums []int, m int) int {
157+
mx := -1
158+
for _, num := range nums {
159+
mx = max(mx, num)
160+
}
161+
left, right := mx, int(1e9)
162+
for left < right {
163+
mid := (left + right) >> 1
164+
if check(nums, m, mid) {
165+
right = mid
166+
} else {
167+
left = mid + 1
168+
}
169+
}
170+
return left
171+
}
172+
173+
func check(nums []int, m, x int) bool {
174+
s, cnt := 0, 1
175+
for _, num := range nums {
176+
if s+num > x {
177+
cnt++
178+
s = num
179+
} else {
180+
s += num
181+
}
182+
}
183+
return cnt <= m
184+
}
185+
186+
func max(a, b int) int {
187+
if a > b {
188+
return a
189+
}
190+
return b
191+
}
69192
```
70193

71194
### **...**

solution/0400-0499/0410.Split Array Largest Sum/README_EN.md

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,142 @@ where the largest sum among the two subarrays is only 18.
4343
<li><code>1 &lt;= m &lt;= min(50, nums.length)</code></li>
4444
</ul>
4545

46-
4746
## Solutions
4847

48+
Binary search.
49+
4950
<!-- tabs:start -->
5051

5152
### **Python3**
5253

5354
```python
54-
55+
class Solution:
56+
def splitArray(self, nums: List[int], m: int) -> int:
57+
def check(x):
58+
s, cnt = 0, 1
59+
for num in nums:
60+
if s + num > x:
61+
cnt += 1
62+
s = num
63+
else:
64+
s += num
65+
return cnt <= m
66+
67+
left, right = max(nums), sum(nums)
68+
while left < right:
69+
mid = (left + right) >> 1
70+
if check(mid):
71+
right = mid
72+
else:
73+
left = mid + 1
74+
return left
5575
```
5676

5777
### **Java**
5878

5979
```java
80+
class Solution {
81+
public int splitArray(int[] nums, int m) {
82+
int mx = -1;
83+
for (int num : nums) {
84+
mx = Math.max(mx, num);
85+
}
86+
int left = mx, right = (int) 1e9;
87+
while (left < right) {
88+
int mid = (left + right) >> 1;
89+
if (check(nums, m, mid)) {
90+
right = mid;
91+
} else {
92+
left = mid + 1;
93+
}
94+
}
95+
return left;
96+
}
97+
98+
private boolean check(int[] nums, int m, int x) {
99+
int s = 0, cnt = 1;
100+
for (int num : nums) {
101+
if (s + num > x) {
102+
++cnt;
103+
s = num;
104+
} else {
105+
s += num;
106+
}
107+
}
108+
return cnt <= m;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int splitArray(vector<int>& nums, int m) {
119+
int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9;
120+
while (left < right) {
121+
int mid = left + right >> 1;
122+
if (check(nums, m, mid)) right = mid;
123+
else left = mid + 1;
124+
}
125+
return left;
126+
}
127+
128+
bool check(vector<int>& nums, int m, int x) {
129+
int s = 0, cnt = 1;
130+
for (int num : nums) {
131+
if (s + num > x) {
132+
++cnt;
133+
s = num;
134+
} else {
135+
s += num;
136+
}
137+
}
138+
return cnt <= m;
139+
}
140+
};
141+
```
60142

143+
### **Go**
144+
145+
```go
146+
func splitArray(nums []int, m int) int {
147+
mx := -1
148+
for _, num := range nums {
149+
mx = max(mx, num)
150+
}
151+
left, right := mx, int(1e9)
152+
for left < right {
153+
mid := (left + right) >> 1
154+
if check(nums, m, mid) {
155+
right = mid
156+
} else {
157+
left = mid + 1
158+
}
159+
}
160+
return left
161+
}
162+
163+
func check(nums []int, m, x int) bool {
164+
s, cnt := 0, 1
165+
for _, num := range nums {
166+
if s+num > x {
167+
cnt++
168+
s = num
169+
} else {
170+
s += num
171+
}
172+
}
173+
return cnt <= m
174+
}
175+
176+
func max(a, b int) int {
177+
if a > b {
178+
return a
179+
}
180+
return b
181+
}
61182
```
62183

63184
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int splitArray(vector<int>& nums, int m) {
4+
int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9;
5+
while (left < right) {
6+
int mid = left + right >> 1;
7+
if (check(nums, m, mid)) right = mid;
8+
else left = mid + 1;
9+
}
10+
return left;
11+
}
12+
13+
bool check(vector<int>& nums, int m, int x) {
14+
int s = 0, cnt = 1;
15+
for (int num : nums) {
16+
if (s + num > x) {
17+
++cnt;
18+
s = num;
19+
} else {
20+
s += num;
21+
}
22+
}
23+
return cnt <= m;
24+
}
25+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
func splitArray(nums []int, m int) int {
2+
mx := -1
3+
for _, num := range nums {
4+
mx = max(mx, num)
5+
}
6+
left, right := mx, int(1e9)
7+
for left < right {
8+
mid := (left + right) >> 1
9+
if check(nums, m, mid) {
10+
right = mid
11+
} else {
12+
left = mid + 1
13+
}
14+
}
15+
return left
16+
}
17+
18+
func check(nums []int, m, x int) bool {
19+
s, cnt := 0, 1
20+
for _, num := range nums {
21+
if s+num > x {
22+
cnt++
23+
s = num
24+
} else {
25+
s += num
26+
}
27+
}
28+
return cnt <= m
29+
}
30+
31+
func max(a, b int) int {
32+
if a > b {
33+
return a
34+
}
35+
return b
36+
}

0 commit comments

Comments
 (0)