Minimum Money Required Before Transactions

You are given a 0-indexed 2D integer array transactions, where transactions[i] = [costi, cashbacki].

The array describes transactions, where each transaction must be completed exactly once in some order. At any given moment, you have a certain amount of money. In order to complete transaction i, money >= costi must hold true. After performing a transaction, money becomes money - costi + cashbacki.

Return the minimum amount of money required before any transaction so that all of the transactions can be completed regardless of the order of the transactions.

Example 1
Inputtransactions = [[2,1],[5,0],[4,2]]
Output10
Starting with money = 10, the transactions can be performed in any order, and starting with money < 10 will fail to complete all transactions in some order.
Example 2
Inputtransactions = [[3,0],[0,3]]
Output3
The order [[3,0],[0,3]] requires 3 money while the order [[0,3],[3,0]] requires 0, so starting with money = 3 works regardless of order.

Constraints

  • 1 <= transactions.length <= 10^5
  • transactions[i].length == 2
  • 0 <= costi, cashbacki <= 10^9

Asked at 1 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