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.
Complete the function minimizeTotalInputCost in the editor.
minimizeTotalInputCost has the following parameters:
- 1.
int[] campaignCosts: an array of integers representing the costs of campaigns - 2.
int numberOfWeeks: the number of weeks
Returns
int: the minimized total input cost
campaignCosts = [1000, 500, 2000, 8000, 1500] numberOfWeeks = 3 return = 9500
Optimal 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 (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.
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. 🍓
campaignCostsis a non-empty array.1 <= numberOfWeeks <= campaignCosts.length- The campaign order must be preserved.
- Count Prime StringsONSITE INTERVIEW · Seen Jun 2026
- Key Teams in TreeOA · Seen Mar 2026
- System Energy ReductionOA · Seen Mar 2026
- Update Logs by Symmetric XOROA · Seen Mar 2026
- Count Palindromic Concatenation PairsOA · Seen Mar 2026
- Collect Opportunity Data in a TreeOA · Seen Feb 2026
- Replace '?' to Avoid Adjacent DuplicatesOA · Seen Feb 2026
- Strings With No k Consecutive Identical CharactersOA · Seen Feb 2026
public int minimizeTotalInputCost(int[] campaignCosts, int numberOfWeeks) {
// write your code here
}