You are given an integer array transactions, where transactions[i] represents the amount of the ith transaction:
The account starts with a balance of 0, and the balance must never become negative. Transactions must be considered in the given order, but you are allowed to skip some transactions.
Return an integer denoting the maximum number of transactions that can be performed without the balance ever going negative.
Example 1:
Input: transactions = [2,-5,3,-1,-2]
Output: 4
Explanation:
One optimal sequence is [2, 3, -1, -2], balance: 0 → 2 → 5 → 4 → 2.
Example 2:
Input: transactions = [-1,-2,-3]
Output: 0
Explanation:
All transactions are negative. Including any would make the balance negative.
Example 3:
Input: transactions = [3,-2,3,-2,1,-1]
Output: 6
Explanation:
All transactions can be taken in order, balance: 0 → 3 → 1 → 4 → 2 → 3 → 2.
Constraints:
1 <= transactions.length <= 105-109 <= transactions[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This problem asks for the maximum number of transactions we can accept in order without the running balance ever dropping below zero. We greedily accept every transaction as it arrives and maintain a min-heap of the negative transactions taken. Whenever the balance becomes negative, we discard the largest negative transaction seen so far to restore a valid balance.
Here's how the algorithm would work step-by-step:
import heapq
def max_transactions(transactions):
# Keep track of the current balance and the min-heap of chosen negative transactions
current_balance = 0
negative_heap = []
count = 0
for amount in transactions:
current_balance += amount
count += 1
# Record any negative transaction we accepted into the heap
if amount < 0:
heapq.heappush(negative_heap, amount)
# If the balance drops below zero, undo the most negative transaction taken so far
if current_balance < 0:
worst_negative = heapq.heappop(negative_heap)
current_balance -= worst_negative
count -= 1
return count
This problem is solved using a greedy approach with a priority queue. We process each transaction in order while maintaining the running balance and a min-heap of all selected negative amounts. Whenever the balance drops below zero, we greedily discard the transaction that caused the largest loss so far to restore non-negativity with the minimal impact on total count.
Here's how the algorithm would work step-by-step:
import heapq
def max_transactions(transactions):
current_balance = 0
chosen_negatives = []
count = 0
# Process transactions in sequential order
for amount in transactions:
current_balance += amount
count += 1
# Track negative transactions in a min-heap to allow backtracking
if amount < 0:
heapq.heappush(chosen_negatives, amount)
# If the balance becomes negative, remove the largest negative transaction
if current_balance < 0:
current_balance -= heapq.heappop(chosen_negatives)
count -= 1
return count
| Case | How to Handle |
|---|---|
| Empty transaction list provided as input | Return zero immediately since no transactions can be performed. |
| Initial balance is already zero and the first transaction is negative | Return zero because the balance drops below zero on the very first step. |
| All transactions are positive additions to the balance | Process the entire list and return the total length of the array. |
| A single large negative transaction that exceeds the initial balance | Stop processing immediately and return a count of zero completed transactions. |
| Transactions contain a mix of zeros, which do not alter the balance | Include zeros in the transaction count since they keep the balance valid and non-negative. |
| Extremely large array of transactions with massive integer values | Use standard linear iteration with a running sum to maintain O(N) time complexity and prevent overflow issues. |
| A valid transaction sequence reduces the balance to exactly zero before continuing | Allow the zero balance state to persist as long as subsequent transactions do not drop it further below zero. |