Problem · Array

Minimize Total Input Cost (for LTMS)

MediumSalesforceOA
See Salesforce hiring insights

Campaigns should be executed in the order they appear. Each week must contain at least one campaign. Weekly input cost is equal to the maximum cost of any campaign in that week. The goal is to minimize the total input cost, which is the sum of weekly input costs.

Function Description

Complete the function minimizeTotalInputCost in the editor.

minimizeTotalInputCost has the following parameters:

  1. 1. int[] campaignCosts: an array of integers representing the costs of campaigns
  2. 2. int numberOfWeeks: the number of weeks

Returns

int: the minimized total input cost

Examples
01 · Example 1
campaignCosts = [1000, 500, 2000, 8000, 1500]
numberOfWeeks = 3
return = 9500
The input might not be right...But it looks correct somehow...

Optimal Allocation:

  1. Week 1: {1000} → Max Cost = 1000
  2. Week 2: {500} → Max Cost = 500
  3. Week 3: {2000, 8000, 1500} → Max Cost = 8000

Output:

  • Weekly Input Costs: {1000, 500, 8000}
  • Total Input Cost (TIC): 1000 + 500 + 8000 = 9500

Explanation:

  • Minimized Weekly Input: The maximum cost in any week is minimized.
  • Optimal Total Input Cost: The sum of all weekly maximum costs (9500) is the smallest possible.

02 · Example 2
campaignCosts = [2, 5, 4, 3, 7, 1, 6, 8]
numberOfWeeks = 3
return = 15

One optimal partition is [2], [5, 4, 3], and [7, 1, 6, 8]. The weekly maxima are 2, 5, and 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
  • campaignCosts is a non-empty array.
  • 1 <= numberOfWeeks <= campaignCosts.length
  • The campaign order must be preserved.
More Salesforce problems
drafts saved locally
public int minimizeTotalInputCost(int[] campaignCosts, int numberOfWeeks) {
  // write your code here
}
campaignCosts[1000, 500, 2000, 8000, 1500]
numberOfWeeks3
expected9500
sign in to submit