Maximum Capacity Within Budget

You are given two integer arrays costs and capacity, both of length n, where costs[i] represents the purchase cost of the i^th machine and capacity[i] represents its performance capacity.

You are also given an integer budget.

You may select at most two distinct machines such that the total cost of the selected machines is strictly less than budget.

Return the maximum achievable total capacity of the selected machines.

Example 1
Inputcosts = [4,8,5,3], capacity = [1,5,2,7], budget = 8
Output8
Choose machines 0 and 3 with total cost 7, which is strictly less than 8, for a total capacity of 8.
Example 2
Inputcosts = [3,5,7,4], capacity = [2,4,3,6], budget = 7
Output6
Choose machine 3 with cost 4, which is strictly less than 7, for a total capacity of 6.

Constraints

  • 1 <= n == costs.length == capacity.length <= 10^5
  • 1 <= costs[i], capacity[i] <= 10^5
  • 1 <= budget <= 2 * 10^5

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate