💰 Minimum Money Required Before Transactions
Intuition 🤔
We need to find the minimum initial money to perform all transactions in any order
Two key factors to consider:

Total loss from losing transactions
\n# Intuition \uD83E\uDD14\n- We need to find the minimum initial money to perform all transactions in any order\n- Two key factors to consider:\n  1. Total loss from losing transactions\n  2. Minimum amount needed for the last transaction\n\n# Approach \uD83C\uDFAF\n1. Calculate total loss from all losing transactions (where cost > cashback)\n2. Find the maximum amount needed for the last transaction:\n   - For losing transactions: need cashback for next transaction\n   - For profitable transactions: need cost to perform transaction\n3. Answer = total_loss + max_cost\n\n# Complexity \u26A1\n- Time complexity: $$O(n)$$\n  - Single pass through all transactions\n- Space complexity: $$O(1)$$\n  - Only using two variables\n\n# Code \uD83D\uDCBB\n```python3\nclass Solution:\n    def minimumMoney(self, transactions: List[List[int]]) -> int:\n        # Track total loss and max cost needed\n        total_loss = 0\n        max_cost = 0\n        \n        # Process each transaction\n        for cost, cashback in transactions:\n            # Add to total loss if it\'s a losing transaction\n            if cost > cashback:\n                total_loss += cost - cashback\n            \n            # Update max cost needed\n            # For losing transactions: need cashback\n            # For profitable transactions: need cost\n            max_cost = max(\n                max_cost,\n                cashback if cost > cashback else cost\n            )\n        \n        # Return minimum initial money needed\n        return total_loss + max_cost\n```\n\n# Additional Notes \uD83D\uDCDD\n1. For losing transactions (cost > cashback):\n   - Add to total_loss\n   - Consider cashback for max_cost\n2. For profitable transactions (cost \u2264 cashback):\n   - No contribution to total_loss\n   - Consider cost for max_cost\n3. The answer ensures:\n   - Enough money to cover all losses\n   - Enough money for any last transaction\n4. Works for any order of transactions\n\n# Example \uD83C\uDF1F\n```python\ntransactions = [[5,2], [3,4], [6,3]]\n\n1. Total loss:\n   [5,2]: loss 3\n   [6,3]: loss 3\n   Total = 6\n\n2. Max cost:\n   [5,2]: cashback = 2\n   [3,4]: cost = 3\n   [6,3]: cashback = 3\n   Max = 3\n\nAnswer = 6 + 3 = 9\n```\n
['Python3']