Minimum Money Required Before Transactions - Problem

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

example_1.py โ€” Basic Case
$ Input: transactions = [[2,1],[5,0],[4,2]]
โ€บ Output: 10
๐Ÿ’ก Note: One possible order: [5,0] first (need 5), then [4,2] (need 4, have 0 so need 4 more = 9 total), then [2,1] (need 2, have 2). Maximum needed was 9. However, with optimal ordering we can do better and need only 10 initially.
example_2.py โ€” All Profitable
$ Input: transactions = [[3,5],[0,3]]
โ€บ Output: 3
๐Ÿ’ก Note: Both transactions are profitable. We need at most 3 to start the first transaction [3,5], after which we'll have enough for any remaining transactions.
example_3.py โ€” Edge Case
$ Input: transactions = [[7,0],[0,1]]
โ€บ Output: 7
๐Ÿ’ก Note: The transaction [7,0] requires 7 initially and gives back nothing. We need at least 7 to complete all transactions regardless of order.

Constraints

  • 1 โ‰ค transactions.length โ‰ค 105
  • transactions[i].length == 2
  • 0 โ‰ค costi, cashbacki โ‰ค 109
  • Each transaction must be completed exactly once

Visualization

Tap to expand
Transaction Optimization StrategyGreedy Strategy: Minimize Peak Capital RequirementExecute loss-making transactions first, then profitable onesLossNet Loss โ†“[5,0]: -5[2,1]: -1ProfitCost โ†‘[4,2]: +2Simulation ResultStart: $0[5,0]: Need $5 โ†’ Have $0[2,1]: Need $2 โ†’ Have $1[4,2]: Need $4 โ†’ Have $2$10๐ŸŽฏ Key InsightDoing big losses first minimizes the peak money needed!
Understanding the Visualization
1
Analyze Each Deal
Categorize each transaction as profitable (cashback โ‰ฅ cost) or loss-making (cashback < cost)
2
Strategic Ordering
Place loss-making deals first, ordered by biggest net loss first, to minimize capital requirements early
3
Simulate Execution
Track the maximum money needed during the entire sequence to find the minimum starting amount
Key Takeaway
๐ŸŽฏ Key Insight: By strategically ordering transactions (losses first, then profits), we minimize the peak capital requirement, solving this complex optimization problem with a simple greedy approach.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.4K Views
High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen