Problem · Array

Minimize Total Input Cost (for LTMS)

Learn this problem
MediumSalesforce logoSalesforceOA
See Salesforce hiring insights

Problem 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) → int

Complete the function minimizeTotalInputCost in the editor.

minimizeTotalInputCost has the following parameters:

  • int[] campaignCosts: an array of integers representing the campaign costs
  • int numberOfWeeks: the number of weeks

Returns

int: the minimized total input cost

Examples

Example 1

campaignCosts = [1000, 500, 2000, 8000, 1500]numberOfWeeks = 3return = 9500

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: 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 = 15

One 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

  • 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
checking account