FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/C++/split-array-largest-sum.cpp at master · Monika-R/LeetCode-Solutions · GitHub
Monika-R
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
C++
/
split-array-largest-sum.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (33 loc) · 855 Bytes
Breadcrumbs
LeetCode-Solutions
/
C++
/
split-array-largest-sum.cpp
Copy path
File metadata and controls
36 lines (33 loc) · 855 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
28
29
30
31
32
33
34
35
36
//
Time: O(nlogs), s is the sum of nums
//
Space: O(1)
class
Solution
{
public:
int
splitArray
(vector<
int
>& nums,
int
m) {
int
left =
0
, right =
0
;
for
(
const
auto
& num : nums) {
left =
max
(left, num);
right += num;
}
while
(left <= right) {
const
auto
mid = left + (right - left) /
2
;
if
(
canSplit
(nums, m, mid)) {
right = mid -
1
;
}
else
{
left = mid +
1
;
}
}
return
left;
}
private:
bool
canSplit
(vector<
int
>& nums,
int
m,
int
sum) {
int
cnt =
1
, curr_sum =
0
;
for
(
const
auto
& num : nums) {
curr_sum += num;
if
(curr_sum > sum) {
curr_sum = num;
++cnt;
}
}
return
cnt <= m;
}
};
Back
|
FazBrowse Home
|
New Git URL