Skip to content

Commit

Permalink
New Problem Soluiton "Split Array Largest Sum"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Nov 13, 2016
1 parent 4d68a13 commit 0941073
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LeetCode
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [C++](./algorithms/cpp/thirdMaximumNumber/ThirdMaximumNumber.cpp)|Easy|
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/) | [C++](./algorithms/cpp/arithmeticSlices/ArithmeticSlices.cpp)|Medium|
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [C++](./algorithms/cpp/fizzBuzz/FizzBuzz.cpp)|Easy|
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) | [C++](./algorithms/cpp/splitArrayLargestSum/SplitArrayLargestSum.cpp)|Hard|
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [C++](./algorithms/cpp/longestPalindrome/LongestPalindrome.cpp)|Easy|
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [C++](./algorithms/cpp/queueReconstructionByHeight/QueueReconstructionByHeight.cpp)|Medium|
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [C++](./algorithms/cpp/convertANumberToHexadecimal/ConvertANumberToHexadecimal.cpp)|Easy|
Expand Down
69 changes: 69 additions & 0 deletions algorithms/cpp/splitArrayLargestSum/SplitArrayLargestSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Source : https://leetcode.com/problems/split-array-largest-sum/
// Author : Hao Chen
// Date : 2016-11-13

/***************************************************************************************
*
* Given an array which consists of non-negative integers and an integer m, you can
* split the array into m non-empty continuous subarrays. Write an algorithm to
* minimize the largest sum among these m subarrays.
*
* Note:
* Given m satisfies the following constraint: 1 ≤ m ≤ length(nums) ≤ 14,000.
*
* Examples:
*
* Input:
* nums = [7,2,5,10,8]
* m = 2
*
* Output:
* 18
*
* Explanation:
* There are four ways to split nums into two subarrays.
* The best way is to split it into [7,2,5] and [10,8],
* where the largest sum among the two subarrays is only 18.
***************************************************************************************/

class Solution {
public:
// Idea
// 1) The max of the result is the sum of the whole array.
// 2) The min of the result is the max num among the array.
// 3) Then, we use Binary Search to find the resullt between the `min` and the `max`

int splitArray(vector<int>& nums, int m) {
int min = 0, max = 0;
for (int n : nums) {
min = std::max(min, n);
max += n;
}
while (min < max) {
int mid = min + (max - min) / 2;
if (hasSmallerSum(nums, m, mid)) max = mid;
else min = mid + 1;
}
return min;
}


// Using a specific `sum` to check wheter we can get `smaller sum`
// The idea here as below:
// find all of possible `sub array` whose sum greater than `sum`
// 1) if the number of `sub array` > m, whcih means the actual result is greater than `sum`
// 2) if the number of `sub array` <= m, whcih means we can have `smaller sum`
//
bool hasSmallerSum(vector<int>& nums, int m, int sum) {
int cnt = 1, curSum = 0;
for (int n : nums) {
curSum += n;
if (curSum > sum) {
curSum = n;
cnt++;
if (cnt > m) return false;
}
}
return true;
}
};

0 comments on commit 0941073

Please sign in to comment.