Problem · Array

Maximum Barbell Weight

Learn this problem
HardSalesforce logoSalesforceFULLTIMEOA
See Salesforce hiring insights

Problem statement

An athlete is loading plates onto a barbell with maximum capacity maxCapacity. The weight of each available plate is given by weights[i].

Choose any subset of the plates, using each plate at most once. Return the maximum total plate weight that does not exceed maxCapacity.

Function

weightCapacity(weights: int[], maxCapacity: int) → int

Examples

Example 1

weights = [7,1,5,6,2]maxCapacity = 7return = 7

There are three optimal ways to reach total weight 7: choose [7], [1, 6], or [2, 5]. Return 7.

Constraints

  • 1 <= weights.length <= 42
  • 1 <= maxCapacity <= 10^9
  • 1 <= weights[i] <= 10^9

More Salesforce problems

drafts saved locally
public int weightCapacity(int[] weights, int maxCapacity) {
    // write your code here
}
weights[7,1,5,6,2]
maxCapacity7
expected7
checking account