|
1 | 1 | /** |
2 | 2 | * @file |
3 | | - * @brief Given an integer array nums and an integer k, split nums into k |
4 | | - * non-empty subarrays such that the largest sum of any subarray is minimized. |
5 | | - * Return the minimized largest sum of the split. |
6 | | -
|
| 3 | + * @brief [Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) |
| 4 | + * |
| 5 | + * Given an integer array `nums` and an integer `k`, this program splits the |
| 6 | + * array into `k` non-empty subarrays such that the largest sum of any subarray is minimized. |
| 7 | + * This function returns the minimized largest sum of the split. |
| 8 | + * |
7 | 9 | * @details |
| 10 | + * ### Problem Explanation: |
8 | 11 | * Example 1: |
9 | | -
|
10 | | - * Input: nums = [7,2,5,10,8], k = 2 |
11 | | - * Output: 18 |
12 | | - * Explanation: There are four ways to split nums into two subarrays. |
13 | | - * The best way is to split it into [7,2,5] and [10,8], where the largest sum |
14 | | - * among the two subarrays is only 18. |
15 | | - |
| 12 | + * - Input: nums = [7,2,5,10,8], k = 2 |
| 13 | + * - Output: 18 |
| 14 | + * - Explanation: The best way to split it into two subarrays is [7,2,5] and [10,8], |
| 15 | + * where the largest sum among the two subarrays is 18. |
| 16 | + * |
16 | 17 | * Example 2: |
17 | | -
|
18 | | - * Input: nums = [1,2,3,4,5], k = 2 |
19 | | - * Output: 9 |
20 | | - * Explanation: There are four ways to split nums into two subarrays. |
21 | | - * The best way is to split it into [1,2,3] and [4,5], where the largest sum |
22 | | - * among the two subarrays is only 9. |
23 | | -
|
24 | | - * @author [ARYA PRATAP SINGH](https://github.com/ARYPROGRAMMER) |
25 | | - * [Leetcode](https://leetcode.com/problems/split-array-largest-sum/description/?envType=problem-list-v2&envId=dynamic-programming) |
| 18 | + * - Input: nums = [1,2,3,4,5], k = 2 |
| 19 | + * - Output: 9 |
| 20 | + * - Explanation: The best way to split it into two subarrays is [1,2,3] and [4,5], |
| 21 | + * where the largest sum among the two subarrays is 9. |
| 22 | + * |
| 23 | + * ### Approach: |
| 24 | + * Dynamic Programming is used to minimize the largest subarray sum by |
| 25 | + * splitting the array into `k` subarrays. |
| 26 | + * |
| 27 | + * ### Time Complexity: |
| 28 | + * O(n^2 * k), where `n` is the size of the array and `k` is the number of subarrays. |
| 29 | + * |
| 30 | + * ### Space Complexity: |
| 31 | + * O(n * k), for the DP table. |
| 32 | + * |
| 33 | + * @author |
| 34 | + * [ARYA PRATAP SINGH](https://github.com/ARYPROGRAMMER) |
26 | 35 | */ |
27 | 36 |
|
28 | | -// header files |
29 | 37 | #include <iostream> |
30 | | -#include <iomanip> |
31 | | -#include <cstdint> |
| 38 | +#include <vector> |
| 39 | +#include <climits> |
| 40 | +#include <cstring> |
| 41 | +#include <cassert> |
32 | 42 |
|
33 | | -/** |
34 | | - * @namespace dynamic_programming |
35 | | - * @brief Dynamic Programming algorithms |
36 | | - */ |
37 | | -namespace dynamic_programming{ |
38 | | - /** |
39 | | - * @namespace split_array_largest_sum |
40 | | - * @brief split_array_largest_sum algorithm |
41 | | - */ |
42 | | - namespace split_array_largest_sum{ |
43 | | - |
44 | | - /** |
45 | | - * @brief This function calculates the minimum largest sum of the split |
46 | | - * @param nums integer array |
47 | | - * @param k integer |
48 | | - * @ |
49 | | - */ |
| 43 | +namespace dynamic_programming { |
| 44 | + namespace split_array_largest_sum { |
50 | 45 |
|
| 46 | + // DP table for memoization |
51 | 47 | int dp[1003][53]; |
52 | 48 |
|
53 | | - int f(int i, int j, vector<int>& v) { |
54 | | - if (j < 0) { |
55 | | - if (i < 0) |
56 | | - return -1; |
57 | | - return INT_MAX; |
58 | | - } |
59 | | - if (i < 0) |
60 | | - return INT_MAX; |
61 | | - if (dp[i][j] != -1) |
62 | | - return dp[i][j]; |
| 49 | + /** |
| 50 | + * @brief Recursive function to calculate minimum largest sum of split |
| 51 | + * @param i current index in the array |
| 52 | + * @param j number of subarrays remaining |
| 53 | + * @param v reference to input vector of numbers |
| 54 | + * @return minimized largest sum for current split configuration |
| 55 | + */ |
| 56 | + int f(int i, int j, const std::vector<int> &v) { |
| 57 | + if (j < 0) return (i < 0) ? -1 : INT_MAX; |
| 58 | + if (i < 0) return INT_MAX; |
| 59 | + if (dp[i][j] != -1) return dp[i][j]; |
63 | 60 |
|
64 | 61 | int res = INT_MAX, sum = 0; |
65 | | - for (int k = i; k >= 0; k--) { |
| 62 | + for (int k = i; k >= 0; --k) { |
66 | 63 | sum += v[k]; |
67 | | - res = min(res, max(sum, f(k - 1, j - 1, v))); |
| 64 | + res = std::min(res, std::max(sum, f(k - 1, j - 1, v))); |
68 | 65 | } |
69 | | - |
70 | 66 | return dp[i][j] = res; |
71 | | - } |
| 67 | + } |
72 | 68 |
|
73 | | - int splitArray(vector<int>& nums, int k) { |
74 | | - memset(dp, -1, sizeof(dp)); |
| 69 | + /** |
| 70 | + * @brief Function to split array and find minimized largest sum |
| 71 | + * @param nums vector of integers representing the array |
| 72 | + * @param k number of subarrays |
| 73 | + * @return minimized largest sum of any subarray after split |
| 74 | + */ |
| 75 | + int splitArray(const std::vector<int> &nums, int k) { |
| 76 | + std::memset(dp, -1, sizeof(dp)); |
75 | 77 | return f(nums.size() - 1, k - 1, nums); |
76 | 78 | } |
77 | 79 | } |
78 | 80 | } |
79 | 81 |
|
80 | 82 | /** |
81 | | - * Test Function |
82 | | - * @return void |
| 83 | + * @brief Test Function |
| 84 | + * This function tests the `splitArray` function by using custom input. |
83 | 85 | */ |
84 | 86 | static void test() { |
85 | | - // custom input vector |
86 | | - std::vector<int> v{ |
87 | | - 7,2,5,10,8,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 |
88 | | - }; |
89 | | - // custom value of k |
| 87 | + std::vector<int> test_array = {7, 2, 5, 10, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
90 | 88 | int k = 5; |
91 | | - |
92 | | - //calling the function |
93 | | - int ans = dynamic_programming::split_array_largest_sum::splitArray(v, k); |
94 | | - |
95 | | - // expected output |
96 | | - int expectedOutput = 30; |
97 | 89 |
|
98 | | - // Testing implementation via assert function |
99 | | - // It will throw error if any of the expected test fails |
100 | | - // Else it will give nothing |
101 | | - assert(ans == expectedOutput); |
| 90 | + // Calling the function |
| 91 | + int result = dynamic_programming::split_array_largest_sum::splitArray(test_array, k); |
| 92 | + |
| 93 | + // Expected output |
| 94 | + int expected = 30; |
102 | 95 |
|
| 96 | + // Testing using assert |
| 97 | + assert(result == expected); |
103 | 98 | std::cout << "All tests passed successfully!\n"; |
104 | | - return; |
105 | 99 | } |
106 | 100 |
|
107 | | -/** Main function (driver code)*/ |
108 | | -int main() { |
109 | | - // test for implementation |
110 | | - test(); |
111 | | - |
112 | | - // user input |
| 101 | +/** |
| 102 | + * @brief Function to handle user input and output. |
| 103 | + * This function gathers input from the user and calls the `splitArray` function. |
| 104 | + */ |
| 105 | +void handleUserIO() { |
113 | 106 | int n; |
114 | | - std::cout << "Enter the number of elements in the array : "; |
| 107 | + std::cout << "Enter the number of elements in the array: "; |
115 | 108 | std::cin >> n; |
116 | | - std::vector<int> v(n); |
117 | | - std::cout << "Enter the elements of the array : "; |
118 | | - for (int i = 0; i < n; i++) { |
119 | | - std::cin >> v[i]; |
| 109 | + |
| 110 | + std::vector<int> nums(n); |
| 111 | + std::cout << "Enter the elements of the array: "; |
| 112 | + for (int &num : nums) { |
| 113 | + std::cin >> num; |
120 | 114 | } |
| 115 | + |
121 | 116 | int k; |
122 | | - std::cout << "Enter the value of k : "; |
| 117 | + std::cout << "Enter the value of k: "; |
123 | 118 | std::cin >> k; |
124 | 119 |
|
125 | | - int ans; |
| 120 | + int result = dynamic_programming::split_array_largest_sum::splitArray(nums, k); |
| 121 | + std::cout << "The minimum largest sum of the split is: " << result << std::endl; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * @brief Main Function (driver code) |
| 126 | + * This function calls test cases and handles user input/output. |
| 127 | + */ |
| 128 | +int main() { |
| 129 | + // Running test cases |
| 130 | + test(); |
| 131 | + |
| 132 | + // Handling user input/output |
| 133 | + handleUserIO(); |
126 | 134 |
|
127 | | - // user output |
128 | | - ans = dynamic_programming::split_array_largest_sum::splitArray(v, k); |
129 | | - std::cout << "The minimum largest sum of the split is : " << ans << std::endl; |
130 | 135 | return 0; |
131 | 136 | } |
0 commit comments