forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0410.Split Array Largest Sum
- Loading branch information
Showing
6 changed files
with
349 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
solution/0400-0499/0410.Split Array Largest Sum/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.