Cutting Metal Surplus
Learn this problemProblem statement
The owner of a construction company has a surplus of rods of arbitrary lengths. A local contractor offers
to buy any of the surplus, as long as all the rods have the same exact integer length, referred to as saleLength.
The number of sellable rods can be increased by cutting each rod zero or more times, but each cut has a cost denoted by costPerCut.
After all cuts have been made, any leftover rods having a length other than saleLength must be discarded for no profit.
The owner's total profit for the sale is calculated as:
totalProfit = totalUniFormRods * saleLength * salePrice - totalCuts * costPerCut
where totalUniformRods is the number of sellable rods, salePrice is the per unit
length price that the contractor agrees to pay, and the totalCuts is the total number of times the rods needed to be cut.
Function
maxProfit(lengths: int[], costPerCut: int, salePrice: int) → intComplete the function maxProfit in the editor.
maxProfit has the following parameter(s):
costPerCut: cost to make a cut salePrice : per unit length sales pricelengths[n]: integer rod lengthsReturns
int: maximum possible profitExamples
Example 1
lengths = [30, 59, 110]costPerCut = 1salePrice = 10return = 1913
Example 2
lengths = [26, 103, 59]costPerCut = 100salePrice = 10return = 1230
Constraints
1 <= n <= 50 1 <= lengths[i] <= 104 1 <= salePrice, costPerCut <= 100 More Goldman Sachs problems
- Data ReorganizationSeen Jul 2026
- Inherited Role PermissionsONSITE INTERVIEW · Seen Jul 2026
- Root of the Largest TreePHONE SCREEN · Seen Jul 2026
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jul 2026
- Alternating Parity PermutationsOA · Seen Jul 2026
- Threshold AlertsSeen Jul 2026
- Cheapest Flights Within K StopsONSITE INTERVIEW · Seen Jun 2026
- Word LadderPHONE SCREEN · Seen Jun 2026