-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat:Added Implementation of Split_Largest_Array_Sum Algorithm #2832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b890d4d
feat:Added Implementation of Split_Largest_Array_Sum Algorithm
ARYPROGRAMMER 745c741
Merge branch 'master' into master
ARYPROGRAMMER b88773a
Update split_array_largest_sum.cpp
ARYPROGRAMMER f6da700
Merge branch 'TheAlgorithms:master' into master
ARYPROGRAMMER bfb0cc1
Merge branch 'master' into master
realstealthninja 829fbbe
Update split_array_largest_sum.cpp
ARYPROGRAMMER File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,131 @@ | ||
/** | ||
* @file | ||
* @brief Given an integer array nums and an integer k, split nums into k | ||
* non-empty subarrays such that the largest sum of any subarray is minimized. | ||
* Return the minimized largest sum of the split. | ||
|
||
* @details | ||
* Example 1: | ||
|
||
* Input: nums = [7,2,5,10,8], k = 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. | ||
|
||
* Example 2: | ||
|
||
* Input: nums = [1,2,3,4,5], k = 2 | ||
* Output: 9 | ||
* Explanation: There are four ways to split nums into two subarrays. | ||
* The best way is to split it into [1,2,3] and [4,5], where the largest sum | ||
* among the two subarrays is only 9. | ||
|
||
* @author [ARYA PRATAP SINGH](https://github.com/ARYPROGRAMMER) | ||
* [Leetcode](https://leetcode.com/problems/split-array-largest-sum/description/?envType=problem-list-v2&envId=dynamic-programming) | ||
*/ | ||
|
||
// header files | ||
#include <iostream> | ||
ARYPROGRAMMER marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <iomanip> | ||
#include <cstdint> | ||
|
||
/** | ||
* @namespace dynamic_programming | ||
* @brief Dynamic Programming algorithms | ||
*/ | ||
namespace dynamic_programming{ | ||
/** | ||
* @namespace split_array_largest_sum | ||
* @brief split_array_largest_sum algorithm | ||
*/ | ||
namespace split_array_largest_sum{ | ||
|
||
/** | ||
* @brief This function calculates the minimum largest sum of the split | ||
* @param nums integer array | ||
* @param k integer | ||
* @ | ||
*/ | ||
|
||
int dp[1003][53]; | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
int f(int i, int j, vector<int>& v) { | ||
if (j < 0) { | ||
if (i < 0) | ||
return -1; | ||
return INT_MAX; | ||
} | ||
if (i < 0) | ||
return INT_MAX; | ||
if (dp[i][j] != -1) | ||
return dp[i][j]; | ||
|
||
int res = INT_MAX, sum = 0; | ||
for (int k = i; k >= 0; k--) { | ||
sum += v[k]; | ||
res = min(res, max(sum, f(k - 1, j - 1, v))); | ||
} | ||
|
||
return dp[i][j] = res; | ||
} | ||
|
||
int splitArray(vector<int>& nums, int k) { | ||
memset(dp, -1, sizeof(dp)); | ||
return f(nums.size() - 1, k - 1, nums); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Test Function | ||
* @return void | ||
*/ | ||
static void test() { | ||
// custom input vector | ||
std::vector<int> v{ | ||
7,2,5,10,8,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 | ||
}; | ||
// custom value of k | ||
int k = 5; | ||
|
||
//calling the function | ||
int ans = dynamic_programming::split_array_largest_sum::splitArray(v, k); | ||
|
||
// expected output | ||
int expectedOutput = 30; | ||
|
||
ARYPROGRAMMER marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// Testing implementation via assert function | ||
// It will throw error if any of the expected test fails | ||
// Else it will give nothing | ||
assert(ans == expectedOutput); | ||
|
||
std::cout << "All tests passed successfully!\n"; | ||
return; | ||
} | ||
|
||
/** Main function (driver code)*/ | ||
int main() { | ||
// test for implementation | ||
test(); | ||
|
||
// user input | ||
int n; | ||
std::cout << "Enter the number of elements in the array : "; | ||
std::cin >> n; | ||
std::vector<int> v(n); | ||
std::cout << "Enter the elements of the array : "; | ||
for (int i = 0; i < n; i++) { | ||
std::cin >> v[i]; | ||
} | ||
int k; | ||
std::cout << "Enter the value of k : "; | ||
std::cin >> k; | ||
|
||
int ans; | ||
|
||
// user output | ||
ans = dynamic_programming::split_array_largest_sum::splitArray(v, k); | ||
std::cout << "The minimum largest sum of the split is : " << ans << std::endl; | ||
ARYPROGRAMMER marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.