Maximizing Profit in Ribbon Cutting with Minimized Costs
Last Updated :
23 Dec, 2023
Given a collection of ribbons, each of which has a certain length and can be sold at a specific price. However, to sell we need to make sure they are of equal length but there’s a cost associated with cutting the ribbons into smaller pieces. The objective is to maximize the total profit by selecting an optimal ribbon length for cutting and making the necessary cuts while minimizing the total cutting cost.
Examples:
Input: lengths = [30, 59, 110], costPerCut = 1, salePrice 10 per unit length
Output: 1913
Explanation: Total uniform ribbons: 3 (all ropes are cut to length 30),Total cuts made: 3 (two cuts of 30 to 59 and one cut of 59 to 110),Total profit = (3 x 30 x 10) – (3 x 1) = 900 – 3 = 897. Therefore, the maximum profit achievable is 897.
Input: lengths = [26, 103, 59], costPerCut = 1, salePrice 10 per unit length
Output: 1770
Approach: The problem can be solved using the following approach:
The approach calculates the maximum profit by iterating through possible sale lengths, simulating the cutting and selling of ribbons while considering the cost of cuts, and maximizing the profit based on uniform ribbon pieces obtained and total cuts needed.
Steps to solve the problem:
- Sort the array “lengths” in ascending order.
- Initialize “maxProfit” to 0 to keep track of the maximum profit.
- Iterate through possible sale lengths from 1 to the maximum length in “lengths.”
- For each sale length, initialize “totalUniformRibbons” and “totalCuts” to 0.
- Iterate through each ribbon’s length in “lengths“:
- Calculate how many uniform ribbons of the current sale length can be obtained (pieces).
- Calculate the leftover length after cutting (leftover).
- Update “totalUniformRibbons” by adding “pieces” and “totalCuts” by adding the maximum of (pieces – 1) or 0 (cuts needed for each ribbon).
- If there is any leftover length, increment “totalCuts” by 1 (additional cut to discard the leftover).
- Calculate the total profit for the current sale length using the formula: totalProfit = totalUniformRibbons * saleLength * salePrice – totalCuts * costPerCut.
- Update “maxProfit” by taking the maximum of the current “maxProfit” and “totalProfit.”
- Return “maxProfit” as the result.
Below is the implementation of the approach:
C++
#include <iostream>
#include <algorithm>
#include <vector>
class RibbonCuttingProfit {
public :
static int maximizeProfit( int costPerCut, int salePrice, std::vector< int >& ribbonLengths) {
std::sort(ribbonLengths.begin(), ribbonLengths.end());
int maxProfit = 0;
for ( int saleLength = 1; saleLength <= ribbonLengths.back(); saleLength++) {
int totalUniformRibbons = 0;
int totalCuts = 0;
for ( int length : ribbonLengths) {
int pieces = length / saleLength;
int leftover = length % saleLength;
totalUniformRibbons += pieces;
totalCuts += std::max(pieces - 1, 0);
if (leftover > 0) {
totalCuts++;
}
}
int totalProfit = totalUniformRibbons * saleLength * salePrice - totalCuts * costPerCut;
maxProfit = std::max(maxProfit, totalProfit);
}
return maxProfit;
}
};
int main() {
int salePrice = 10;
std::vector< int > ribbonLengths = {26, 103, 59};
int costPerCut = 1;
int result = RibbonCuttingProfit::maximizeProfit(costPerCut, salePrice, ribbonLengths);
std::cout << result << std::endl;
return 0;
}
|
Java
import java.util.Arrays;
public class RibbonCuttingProfit {
public static int maximizeProfit( int costPerCut,
int salePrice,
int [] ribbonLengths)
{
Arrays.sort(ribbonLengths);
int maxProfit = 0 ;
for ( int saleLength = 1 ;
saleLength
<= ribbonLengths[ribbonLengths.length - 1 ];
saleLength++) {
int totalUniformRibbons = 0 ;
int totalCuts = 0 ;
for ( int length : ribbonLengths) {
int pieces = length / saleLength;
int leftover = length % saleLength;
totalUniformRibbons += pieces;
totalCuts += Math.max(pieces - 1 , 0 );
if (leftover > 0 ) {
totalCuts++;
}
}
int totalProfit = totalUniformRibbons
* saleLength * salePrice
- totalCuts * costPerCut;
maxProfit = Math.max(maxProfit, totalProfit);
}
return maxProfit;
}
public static void main(String[] args)
{
int salePrice = 10 ;
int [] ribbonLengths = { 26 , 103 , 59 };
int costPerCut = 1 ;
int result = maximizeProfit(costPerCut, salePrice,
ribbonLengths);
System.out.println(result);
}
}
|
Python3
class RibbonCuttingProfit:
def maximizeProfit(costPerCut, salePrice, ribbonLengths):
ribbonLengths.sort()
maxProfit = 0
for saleLength in range ( 1 , ribbonLengths[ - 1 ] + 1 ):
totalUniformRibbons = 0
totalCuts = 0
for length in ribbonLengths:
pieces = length / / saleLength
leftover = length % saleLength
totalUniformRibbons + = pieces
totalCuts + = max (pieces - 1 , 0 )
if leftover > 0 :
totalCuts + = 1
totalProfit = totalUniformRibbons * saleLength * \
salePrice - totalCuts * costPerCut
maxProfit = max (maxProfit, totalProfit)
return maxProfit
salePrice = 10
ribbonLengths = [ 26 , 103 , 59 ]
costPerCut = 1
result = RibbonCuttingProfit.maximizeProfit(
costPerCut, salePrice, ribbonLengths)
print (result)
|
C#
using System;
public class RibbonCuttingProfit {
public static int MaximizeProfit( int costPerCut,
int salePrice,
int [] ribbonLengths)
{
Array.Sort(ribbonLengths);
int maxProfit = 0;
for ( int saleLength = 1;
saleLength
<= ribbonLengths[ribbonLengths.Length - 1];
saleLength++) {
int totalUniformRibbons = 0;
int totalCuts = 0;
foreach ( int length in ribbonLengths)
{
int pieces = length / saleLength;
int leftover = length % saleLength;
totalUniformRibbons += pieces;
totalCuts += Math.Max(pieces - 1, 0);
if (leftover > 0) {
totalCuts++;
}
}
int totalProfit = totalUniformRibbons
* saleLength * salePrice
- totalCuts * costPerCut;
maxProfit = Math.Max(maxProfit, totalProfit);
}
return maxProfit;
}
public static void Main( string [] args)
{
int salePrice = 10;
int [] ribbonLengths = { 26, 103, 59 };
int costPerCut = 1;
int result = MaximizeProfit(costPerCut, salePrice,
ribbonLengths);
Console.WriteLine(result);
}
}
|
Javascript
class GFG {
static maximizeProfit(costPerCut, salePrice, ribbonLengths) {
ribbonLengths.sort((a, b) => a - b);
let maxProfit = 0;
for (let saleLength = 1; saleLength <= ribbonLengths[ribbonLengths.length - 1]; saleLength++) {
let totalUniformRibbons = 0;
let totalCuts = 0;
for (let length of ribbonLengths) {
let pieces = Math.floor(length / saleLength);
let leftover = length % saleLength;
totalUniformRibbons += pieces;
totalCuts += Math.max(pieces - 1, 0);
if (leftover > 0) {
totalCuts++;
}
}
let totalProfit = totalUniformRibbons * saleLength * salePrice - totalCuts * costPerCut;
maxProfit = Math.max(maxProfit, totalProfit);
}
return maxProfit;
}
}
const salePrice = 10;
const ribbonLengths = [26, 103, 59];
const costPerCut = 1;
const result = GFG.maximizeProfit(costPerCut, salePrice, ribbonLengths);
console.log(result);
|
Time Complexity: O(max_length * n), where max_length is the maximum length in the lengths array, and n is the number of elements in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...