Minimize Total Input Cost (for LTMS)
Learn this problemProblem statement
Given an array of campaign costs and a number of weeks, partition the campaigns to minimize the sum of weekly maximum costs.
Campaigns must be executed in the order they appear, and each week must contain at least one campaign. The input cost for a week is the maximum campaign cost assigned to that week.
Return the minimum possible sum of weekly maximum costs.
Function
minimizeTotalInputCost(campaignCosts: int[], numberOfWeeks: int) → intComplete the function minimizeTotalInputCost in the editor.
minimizeTotalInputCost has the following parameters:
int[] campaignCosts: an array of integers representing the campaign costsint numberOfWeeks: the number of weeks
Returns
int: the minimized total input cost
Examples
Example 1
campaignCosts = [1000, 500, 2000, 8000, 1500]numberOfWeeks = 3return = 9500Optimal Allocation:
- Week 1:
{1000}→ Max Cost =1000 - Week 2:
{500}→ Max Cost =500 - Week 3:
{2000, 8000, 1500}→ Max Cost =8000
Output:
- Weekly input costs:
{1000, 500, 8000} - Total input cost:
1000 + 500 + 8000 = 9500
The sum of all weekly maximum costs is minimized, so the answer is 9500.
Example 2
campaignCosts = [2, 5, 4, 3, 7, 1, 6, 8]numberOfWeeks = 3return = 15One optimal partition is:
[2], weekly maximum =2[5, 4, 3], weekly maximum =5[7, 1, 6, 8], weekly maximum =8
So the minimized total is 2 + 5 + 8 = 15.
Note: This example was added on June 21, 2026 from a newly reported Salesforce onsite question variant. 🍓
Constraints
campaignCostsis a non-empty array.1 <= numberOfWeeks <= campaignCosts.length- The campaign order must be preserved.