Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0410.Split Array Largest Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Aug 7, 2021
1 parent 35ef049 commit b9593eb
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 21 deletions.
127 changes: 125 additions & 2 deletions solution/0400-0499/0410.Split Array Largest Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,150 @@
<li><code>1 <= m <= min(50, nums.length)</code></li>
</ul>


## 解法

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

二分查找。

二分枚举**子数组的和的最大值**,找到满足条件的最小值。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def check(x):
s, cnt = 0, 1
for num in nums:
if s + num > x:
cnt += 1
s = num
else:
s += num
return cnt <= m

left, right = max(nums), sum(nums)
while left < right:
mid = (left + right) >> 1
if check(mid):
right = mid
else:
left = mid + 1
return left
```

### **Java**

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

```java
class Solution {
public int splitArray(int[] nums, int m) {
int mx = -1;
for (int num : nums) {
mx = Math.max(mx, num);
}
int left = mx, right = (int) 1e9;
while (left < right) {
int mid = (left + right) >> 1;
if (check(nums, m, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}

private boolean check(int[] nums, int m, int x) {
int s = 0, cnt = 1;
for (int num : nums) {
if (s + num > x) {
++cnt;
s = num;
} else {
s += num;
}
}
return cnt <= m;
}
}
```

### **C++**

```cpp
class Solution {
public:
int splitArray(vector<int>& nums, int m) {
int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9;
while (left < right) {
int mid = left + right >> 1;
if (check(nums, m, mid)) right = mid;
else left = mid + 1;
}
return left;
}

bool check(vector<int>& nums, int m, int x) {
int s = 0, cnt = 1;
for (int num : nums) {
if (s + num > x) {
++cnt;
s = num;
} else {
s += num;
}
}
return cnt <= m;
}
};
```

### **Go**

```go
func splitArray(nums []int, m int) int {
mx := -1
for _, num := range nums {
mx = max(mx, num)
}
left, right := mx, int(1e9)
for left < right {
mid := (left + right) >> 1
if check(nums, m, mid) {
right = mid
} else {
left = mid + 1
}
}
return left
}

func check(nums []int, m, x int) bool {
s, cnt := 0, 1
for _, num := range nums {
if s+num > x {
cnt++
s = num
} else {
s += num
}
}
return cnt <= m
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **...**
Expand Down
125 changes: 123 additions & 2 deletions solution/0400-0499/0410.Split Array Largest Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,142 @@ where the largest sum among the two subarrays is only 18.
<li><code>1 &lt;= m &lt;= min(50, nums.length)</code></li>
</ul>


## Solutions

Binary search.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def check(x):
s, cnt = 0, 1
for num in nums:
if s + num > x:
cnt += 1
s = num
else:
s += num
return cnt <= m

left, right = max(nums), sum(nums)
while left < right:
mid = (left + right) >> 1
if check(mid):
right = mid
else:
left = mid + 1
return left
```

### **Java**

```java
class Solution {
public int splitArray(int[] nums, int m) {
int mx = -1;
for (int num : nums) {
mx = Math.max(mx, num);
}
int left = mx, right = (int) 1e9;
while (left < right) {
int mid = (left + right) >> 1;
if (check(nums, m, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}

private boolean check(int[] nums, int m, int x) {
int s = 0, cnt = 1;
for (int num : nums) {
if (s + num > x) {
++cnt;
s = num;
} else {
s += num;
}
}
return cnt <= m;
}
}
```

### **C++**

```cpp
class Solution {
public:
int splitArray(vector<int>& nums, int m) {
int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9;
while (left < right) {
int mid = left + right >> 1;
if (check(nums, m, mid)) right = mid;
else left = mid + 1;
}
return left;
}

bool check(vector<int>& nums, int m, int x) {
int s = 0, cnt = 1;
for (int num : nums) {
if (s + num > x) {
++cnt;
s = num;
} else {
s += num;
}
}
return cnt <= m;
}
};
```

### **Go**

```go
func splitArray(nums []int, m int) int {
mx := -1
for _, num := range nums {
mx = max(mx, num)
}
left, right := mx, int(1e9)
for left < right {
mid := (left + right) >> 1
if check(nums, m, mid) {
right = mid
} else {
left = mid + 1
}
}
return left
}

func check(nums []int, m, x int) bool {
s, cnt := 0, 1
for _, num := range nums {
if s+num > x {
cnt++
s = num
} else {
s += num
}
}
return cnt <= m
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **...**
Expand Down
25 changes: 25 additions & 0 deletions solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int splitArray(vector<int>& nums, int m) {
int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9;
while (left < right) {
int mid = left + right >> 1;
if (check(nums, m, mid)) right = mid;
else left = mid + 1;
}
return left;
}

bool check(vector<int>& nums, int m, int x) {
int s = 0, cnt = 1;
for (int num : nums) {
if (s + num > x) {
++cnt;
s = num;
} else {
s += num;
}
}
return cnt <= m;
}
};
36 changes: 36 additions & 0 deletions solution/0400-0499/0410.Split Array Largest Sum/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
func splitArray(nums []int, m int) int {
mx := -1
for _, num := range nums {
mx = max(mx, num)
}
left, right := mx, int(1e9)
for left < right {
mid := (left + right) >> 1
if check(nums, m, mid) {
right = mid
} else {
left = mid + 1
}
}
return left
}

func check(nums []int, m, x int) bool {
s, cnt := 0, 1
for _, num := range nums {
if s+num > x {
cnt++
s = num
} else {
s += num
}
}
return cnt <= m
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
Loading

0 comments on commit b9593eb

Please sign in to comment.