Launch Wallet Safety
Preview mode. Log in to edit, run, submit, and save progress.
Launch Wallet Safety
A launch wallet must process every vendor transaction. A transaction contains a cost and a cashback amount. Before starting a transaction, the wallet must contain at least the transaction cost. After the transaction is processed, the cashback is immediately returned to the wallet. The transactions can be processed in any order, but the starting wallet must be large enough to guarantee that all transactions can be completed safely. Input Format: You are given an array transactions, where each transaction is [cost, cashback]. Output Format: Return the minimum initial wallet amount required to complete all transactions.
Examples
transactions = [[2, 1], [5, 0], [4, 2]]
10
Explanation: Unavoidable losses are [1, 5, 2], totaling 8. The largest one-transaction checkpoint is 2, so the minimum starting wallet is 8 + 2 = 10.
Approach hint
Look for the state that actually matters after each operation.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.