Minimum Money Required Before Transactions - Problem

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.

Input & Output

Example 1 — Mixed Transactions
$ Input: transactions = [[2,1],[5,0],[4,2]]
Output: 10
💡 Note: One optimal order: [5,0] costs 5, [4,2] costs 4 but we have 0-5+0 = -5 so need 4-(-5) = 9 more, then [2,1]. Total initial money needed: 5 + 9 = 14. Actually, better order gives 10.
Example 2 — All Profitable
$ Input: transactions = [[3,5],[0,3]]
Output: 3
💡 Note: Start with [3,5]: need 3 initially. After: 3-3+5 = 5. Then [0,3]: need 0, have 5. Final money needed: 3.
Example 3 — All Losses
$ Input: transactions = [[7,0],[4,1]]
Output: 7
💡 Note: Do cheaper loss first: [4,1] needs 4 initially, left with 4-4+1=1. Then [7,0] needs 7, have 1, so need 6 more. Total: 4+6=10. Better: [7,0] first needs 7, then [4,1] needs 3 more since we have 0. Total: 7+3=10. Actually optimal gives 7.

Constraints

  • 1 ≤ transactions.length ≤ 105
  • transactions[i].length == 2
  • 0 ≤ costi, cashbacki ≤ 109

Visualization

Tap to expand
Minimum Money Required Before Transactions INPUT transactions array [0]: cost=2, cashback=1 Loss: 2-1 = 1 [1]: cost=5, cashback=0 Loss: 5-0 = 5 (losing) [2]: cost=4, cashback=2 Loss: 4-2 = 2 Classification: Losing: [0],[1],[2] Gaining: none [[2,1],[5,0],[4,2]] 3 transactions total ALGORITHM STEPS 1 Sum all losses totalLoss = 1+5+2 = 8 2 Find max cost (losing) maxCost = max(2,5,4) = 5 3 Find max cashback maxCashback = max(1,0,2) = 2 4 Calculate result totalLoss + max(maxCost-loss, maxCashback) Greedy Formula: ans = 8 + max(5-5, 2) ans = 8 + max(0, 2) ans = 8 + 2 = 10 Worst order: [2,1]-->[5,0]-->[4,2] 10-->9-->4-->2 (OK) FINAL RESULT Minimum Money 10 Verification: Any Order Works with 10: Order [1,0,2]: 10-->5-->4-->2 OK Order [2,0,1]: 10-->8-->7-->2 OK Order [0,1,2]: 10-->9-->4-->2 OK Order [1,2,0]: 10-->5-->3-->2 OK Output: 10 Key Insight: For worst-case ordering, we need money = (sum of all net losses) + (extra for worst starting transaction). Losing transactions (cost > cashback) should be analyzed for maximum cost after other losses. Greedy approach: Track total losses and find the worst additional requirement for any transaction order. TutorialsPoint - Minimum Money Required Before Transactions | Greedy Optimal Strategy
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~35 min Avg. Time
456 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