FastPrepThree-Resource 0/1 Knapsack

Three-Resource 0/1 Knapsack

Morgan Stanley logoMorgan Stanley● MediumINTERNOA
Learn

Problem statement

Each item has a value and consumes three independent resources. Select each item at most once without exceeding any of the three capacities, and return the maximum total value.

Function

maximizeKnapsackValue(values: int[], firstCosts: int[], secondCosts: int[], thirdCosts: int[], firstCapacity: int, secondCapacity: int, thirdCapacity: int) → int

Examples

Example 1

values = [10,20]firstCosts = [1,2]secondCosts = [1,2]thirdCosts = [1,2]firstCapacity = 2secondCapacity = 2thirdCapacity = 2return = 20

Case 1 exercises the documented deterministic contract.

Example 2

values = [6,10,12]firstCosts = [1,2,3]secondCosts = [2,1,2]thirdCosts = [1,2,1]firstCapacity = 4secondCapacity = 4thirdCapacity = 3return = 18

Case 2 exercises the documented deterministic contract.

Example 3

values = [5]firstCosts = [0]secondCosts = [0]thirdCosts = [0]firstCapacity = 0secondCapacity = 0thirdCapacity = 0return = 5

Case 3 exercises the documented deterministic contract.

Constraints

  • 1 <= values.length <= 40.
  • All four item arrays have equal length.
  • 0 <= costs and capacities <= 40.
  • Values are nonnegative and the answer fits a signed 32-bit integer.

More Morgan Stanley problems

See Morgan Stanley hiring insights
public int maximizeKnapsackValue(int[] values, int[] firstCosts, int[] secondCosts, int[] thirdCosts, int firstCapacity, int secondCapacity, int thirdCapacity) {
    // Write your code here.
}
values[10,20]
firstCosts[1,2]
secondCosts[1,2]
thirdCosts[1,2]
firstCapacity2
secondCapacity2
thirdCapacity2
expected20
Checking account…