Imagine you're planning to complete a series of financial transactions, each with a cost upfront and a cashback afterward. The challenge? You need to determine the minimum starting amount that guarantees you can complete all transactions regardless of their order.
You are given a 2D array transactions where transactions[i] = [costi, cashbacki]. Each transaction must be completed exactly once, and at any moment, your money must be at least costi to perform transaction i. After completing it, your money becomes money - costi + cashbacki.
Goal: Return the minimum initial amount needed so that no matter how the transactions are ordered, you can always complete them all.
Example: If transactions = [[2,1],[5,0],[4,2]], you might need different starting amounts depending on the order. The answer is the minimum that works for any possible order.
Input & Output
Constraints
- 1 โค transactions.length โค 105
- transactions[i].length == 2
- 0 โค costi, cashbacki โค 109
- Each transaction must be completed exactly once