Maximum Transactions Without Negative Balance
You are given an integer array transactions, where:
- transactions[i] > 0 represents money credited to your account.
- transactions[i] < 0 represents money debited from your account.
Your account starts with a balance of 0.
You must process transactions in the given order. However, you are allowed to skip any transaction.
At no point should the account balance become negative.
Return the maximum number of transactions you can perform such that the balance never goes below 0.
Example 1:
Input: transactions = [5, -3, 4, -2]
Output: 4
Explanation:
You can process all transactions:
- Balance progression: 0 → 5 → 2 → 6 → 4
The balance never becomes negative.
Example 2:
Input: transactions = [4, -6, 3, -1]
Output: 3
Explanation:
You cannot include -6 because balance would become negative.
- One optimal selection: [4, 3, -1]
- Balance progression: 0 → 4 → 7 → 6
Total transactions performed = 3.
Now Your Turn!
Pick the correct output for the given inputInput: transactions = [6, -4, -3, 5, -2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= transactions.length <= 105
- -109 <= transactions[i] <= 109