#2412

Minimum Money Required Before Transactions

master · 1655 · lc hard +32 · verified · 42% accepted · 415 likes · top 23%

Description

You are given a 0-indexed 2D array transactions where transactions[i] = [costi, cashbacki]. Each transaction requires spending costi money, then receiving cashbacki money back.

Before the first transaction, you need enough money to pay costi. Determine the minimum amount of money you must have at the start to be able to complete all transactions in the worst-case ordering.

Return this minimum starting amount.

Example 1:

Input: transactions = [[2,1],[5,0],[4,2]]
Output: 10
Explanation:
Starting with money = 10, the transactions can be performed in any order.
It can be shown that starting with money < 10 will fail to complete all transactions in some order.

Example 2:

Input: transactions = [[3,0],[0,3]]
Output: 3
Explanation:
- If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3.
- If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0.
Thus, starting with money = 3, the transactions can be performed in any order.

Code

1
2
3