Skip to the code
cf.
+25 XP

#515 Most Experiments Within Two Budgets

medium

A product team wants to run as many A/B experiments as possible this month. Experiment i is given as experiments[i] = [cpu, memory], the server CPU units and memory units it needs. The month's budgets are cpuBudget and memoryBudget, and each experiment is run at most once.

Write mostExperiments(experiments, cpuBudget, memoryBudget) that returns the largest number of experiments whose total CPU stays within cpuBudget and whose total memory stays within memoryBudget.

Example 1

Input: experiments = [[1,2],[1,1],[2,1]], cpuBudget = 3, memoryBudget = 3
Output: 2

Any two fit (for example the first two use 2 CPU and 3 memory), but all three need 4 and 4.

Example 2

Input: experiments = [[2,2],[2,2]], cpuBudget = 1, memoryBudget = 5
Output: 0

Each experiment needs 2 CPU, but the budget is 1.

Constraints

  • 1 <= experiments.length <= 100
  • 0 <= cpu, memory <= 100
  • 0 <= cpuBudget, memoryBudget <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).