Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions C++/RodCutting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

int max(int a, int b) {
return (a > b) ? a : b;
}

int rodCutting(vector<int>& prices, int length) {
if (length <= 0) return 0;

vector<int> dp(length + 1, 0);

for (int i = 1; i <= length; i++) {
int max_price = INT_MIN;
for (int j = 1; j <= i; j++) {
max_price = max(max_price, prices[j - 1] + dp[i - j]);
}
dp[i] = max_price;
}

return dp[length];
}

int main() {
int n;
cout << "Enter the number of pieces: ";
cin >> n;

vector<int> prices(n);
cout << "Enter the prices for each piece:\n";
for (int i = 0; i < n; i++) {
cin >> prices[i];
}

int length;
cout << "Enter the length of the rod: ";
cin >> length;

int max_profit = rodCutting(prices, length);
cout << "Maximum profit: " << max_profit << endl;

return 0;
}
75 changes: 75 additions & 0 deletions C++/SplitArrayLargestSum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Split Array Largest Sum

Given an array nums 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.

### Example 1:

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.

### Example 2:

Input: nums = [1,2,3,4,5], m = 2
Output: 9

Example 3:

Input: nums = [1,4,4], m = 3
Output: 4



### Constraints:

1 <= nums.length <= 1000
0 <= nums[i] <= 106
1 <= m <= min(50, nums.length)


## Approach & Solution

We can break this problem into two smaller problems:

1. Given an array (A), number of cuts (CUTS), and the Largest sum of sub-arrays (MAX). Can you use at most CUTS cuts to segment array A into CUTS + 1 sub-arrays, such that the sum of each sub-array is smaller or equal to MAX?

2. Given a lower bound (left), an upper bound (right), an unknown bool array (B), and an API uses i as input and tells you whether B[i] is true. If we know there exists an index k, that B[i] is false when i < k, and B[i] is true when i >= k. What is the fastest way to find this k (the lower bound)?

Solution to the first sub-problem :

For the first question, we can follow these steps:

1. For each element in the array, if its value is larger than MAX, we know it's not possible to cut this array into groups that the sum of all groups are smaller than MAX. (Reason is straightforward, if A is [10, 2, 3, 5] and MAX is 6, even you have 3 cuts by which you can cut A as [[10], [2], [3], [5]], the group containing 10 will still be larger than 6).

2. Use greedy algorithm to cut A. Use an accumulator ACC to store the sum of the currently processed group, and process elements in A one by one. For each element num, if we add num with ACC and the new sum is still no larger than MAX, we update ACC to ACC + num, which means we can merge num into the current group. If not, we must use a cut before num to segment this array, then num will be the first element in the new group.

3. If we didn't go through A but already used up all cuts, then it's not possible only using CUTS cuts to segment this array into groups to make sure sum of each sub-array is smaller than MAX. Otherwise, if we can reach the end of A with cuts left (or use exactly CUTS cuts). It's possible to do so.

Then the first question is solved.

Solution to the second sub-problem :

1. The array B will be something like [false, false, ..., false, true, true, ..., true]. We want to find the index of the first true.

2. Use binary search to find this k. Keep a value mid, mid = (left + right) / 2. If B[mid] = false, then move the search range to the upper half of the original search range, a.k.a left = mid + 1, otherwise move search range to the lower half, a.k.a right = mid.

**Why this algorithm is correct...**

1. No matter how we cut the array A, the Largest sum of sub-arrays will fall into a range [left, right]. Left is the value of the largest element in this array. right is the sum of this array. (e.g., Given array [1, 2, 3, 4, 5], if we have 4 cuts and cut it as [[1], [2], [3], [4], [5]], the Largest sum of sub-arrays is 5, it cannot be smaller. And if we have 0 cut, and the only sub-array is [[1, 2, 3, 4, 5]], the Largest sum of sub-arrays is 15, it cannot be larger).

2. However, we cannot decide the number of cuts (CUTS), this is an given constraint. But we know there must be a magic number k, which is the smallest value of the Largest sum of sub-arrays when given CUTS cuts. When the Largest sum of sub-arrays is larger than k, we can always find a way to cut A within CUTS cuts. When the Largest sum of sub-arrays is smaller than k, there is no way to do this.

**Example**

For example, given array A [1, 2, 3, 4, 5]. We can use 2 cuts.

1. No matter how many cuts are allowed, the range of the possible value of the Largest sum of sub-arrays is [5, 15].
2. When given 2 cuts, we can tell the magic number k here is 6, the result of segmentation is [[1, 2, 3], [4], [5]].
3. When Largest sum of sub-arrays is in range [6, 15], we can always find a way to cut this array within two cuts. You can have a try.
4. However, when Largest sum of sub-arrays is in range [5, 5], there is no way to do this.
5. This mapped this problem into the second sub-problem. Bool array B here is [5:false, 6:true, 7:true, 8:true, ..., 15:true]. We want to find the index i of the first true in B, which is the answer of this entire question, and by solving the first sub-problem, we have an API that can tell us given an i (Largest sum of sub-arrays), whether B[i] is true (whether we can find a way to cut A to satisfy the constraint).
98 changes: 98 additions & 0 deletions C++/SplitArrayLargestSum/SplitArrayLargestSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//CODE

#include <bits/stdc++.h>

using namespace std;

class Solution {
private:
/*
Parameters:
nums - The input array;
cuts - How many cuts are available (cuts = #groups - 1);
max - The maximum of the (sum of elements in one group);
Return:
Whether we can use at most 'cuts' number of cuts to segment the entire array,
such that the sum of each group will not exceed 'max'.
*/
bool doable (const vector<int>& nums, int cuts, long long max) {

// 'acc' is the temporary accumulator for the currently processed group.

int acc = 0;
for (int num : nums) {

// If the current processed element in this array is larger than 'max', we cannot segment the array.
// (Reason is straightforward, if 'nums' is [10, 2, 3, 5] and 'max' is 6, even you can have 3 cuts
// (by which you can cut array as [[10], [2], [3], [5]]), the group containing 10 will be larger than 6,
// there is no way to do this).
// Ps: This step is unnecessary in this solution. Because 'left' in the splitArray() function can assure
// 'max' will be larger than every single element. I just want to write a generalized doable() function :)

if (num > max) return false;

// If the (sum of the currently processed group) + (current element) is smaller than max, we can add current
// element into this group.

else if (acc + num <= max) acc += num;

// If not, we will make a cut before this element, and this element will be the first element in the new group.

else {
--cuts;
acc = num;

// If we've used up all cuts, this means this 'max' is not doable.
if (cuts < 0) return false;
}
}

// If we can reach here, this means we've used at most 'cuts' cut to segment the array, and the sum of each groups is
// not larger than 'max'. Yeah!
return true;
}

public:
int splitArray(vector<int>& nums, int m) {
// Use long long to avoid overflow.
long long left = 0, right = 0;
// The smallest possible value ('left') is the the value of the largest element in this array.
// The largest possible value ('right') is the sum of all elements in this array.
for (int num : nums) {
left = max(left, (long long)num);
right += num;
}

// Use binary search, find the lower bound of the possible (minimum sum of groups within m - 1 cuts).
while (left < right) {
long long mid = left + (right - left) / 2;
if (doable(nums, m - 1, mid)) right = mid;
else left = mid + 1;
}
return left;
}
};

// Driver Code Starts.
int main()
{
int t;
cout << "Inpur the number of Test cases :";
cin >> t;
while (t--)
{
int n,m;
cout << "Enter the Quantity of Array Elements: ";
cin >> n;
cout << "Input " << n << " Array elements: ";
vector<int> nums(n);
for (auto &it : nums) {
cin >> it;
}
cout << "Enter the value of m: ";
cin >> m;
Solution ob;
cout << "The largest sum among the " << m << " subarray is " << ob.splitArray(nums, m) << endl;
}
return 0;
}
60 changes: 60 additions & 0 deletions C/RodCutting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>

int max(int a, int b) {
return (a > b) ? a : b;
}

int rodCutting(int prices[], int length) {
if (length <= 0) return 0;

int* dp = (int*)malloc((length + 1) * sizeof(int));
if (dp == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}

for (int i = 0; i <= length; i++) {
dp[i] = 0;
}

for (int i = 1; i <= length; i++) {
int max_price = -1;
for (int j = 1; j <= i; j++) {
max_price = max(max_price, prices[j - 1] + dp[i - j]);
}
dp[i] = max_price;
}

int max_profit = dp[length];
free(dp);

return max_profit;
}

int main() {
int n;
printf("Enter the number of pieces: ");
scanf("%d", &n);

int* prices = (int*)malloc(n * sizeof(int));
if (prices == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}

printf("Enter the prices for each piece:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &prices[i]);
}

int length;
printf("Enter the length of the rod: ");
scanf("%d", &length);

int max_profit = rodCutting(prices, length);
printf("Maximum profit: %d\n", max_profit);

free(prices);
return 0;
}