Minimum Postponed Debits to Avoid Negative Balance
Heaps, Sorting & SearchAuto-graded · 24 test casesLast updated
Find the minimum number of negative transactions to postpone so that the running account balance remains non-negative throughout processing.
You are given a chronological sequence of financial transactions represented by an array of integers a. Positive integers represent credits (deposits), negative integers represent debits (withdrawals), and zero values represent neutral entries.
Starting with an initial balance of 0, transactions are processed sequentially. The balance must never drop below 0 after any transaction. To ensure the account balance remains non-negative at all times, you are permitted to postpone certain debit transactions by moving them to the end of the sequence. Postponed transactions will be executed at the very end after all other transactions have been processed.
Return the minimum number of debit transactions that need to be postponed.
Rules
- Only negative values (debits) may be postponed. Neutral values (
0) do not affect the balance and do not need to be postponed. - Non-postponed transactions are processed in their original relative order.
- Postponed debits are executed after all remaining transactions. The overall sum of all elements in
ais guaranteed to be non-negative ($\sum a_i \ge 0$), ensuring that the final sequence will be valid once all debits are paid. - A running balance of exactly
0is allowed at any point.
Constraints
- $1 \le \text{len}(a) \le 10^5$
- $-10^9 \le a[i] \le 10^9$
- $\sum a[i] \ge 0$
Examples
Example 1
Input: min_expenses_to_move([5, -3, -4, 6, -1])
Output: 1
Example 2
Input: min_expenses_to_move([-2, 3, -1, -5, 4, 2])
Output: 2
Example 3
Input: min_expenses_to_move([3, -1, -2, 4])
Output: 0
Hints
Hint 1
Process transactions sequentially while tracking the running balance.
Hint 2
Maintain a priority queue (min-heap) of all debit amounts encountered so far that have not been postponed.
Hint 3
When the running balance falls below 0, which debit should be postponed to regain the largest balance with a single move?
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can process the list of transactions in chronological order while maintaining the running total balance. Whenever we process a debit (negative value), we record it as a potential candidate for postponement in a min-heap.
If at any point the running balance drops below 0, we must rectify the balance deficit. To minimize the total number of postponed debits, we should greedily remove the largest debit (i.e., the most negative number) encountered up to that point. Moving that transaction to the end increases our running balance by its magnitude, providing the maximum possible cushion for subsequent transactions.
Because Python's heapq module implements a min-heap, pushing negative values places the most negative value (largest debit amount) at the top of the heap. When balance < 0, popping from the heap yields the optimal debit to postpone.
Correctness
- Greedy Choice: Each postponed transaction costs 1 operation. Postponing a larger debit gives a larger increase to the prefix sum than a smaller debit. Therefore, selecting the most negative debit so far provides the greatest balance boost per move.
- Feasibility: Since the overall sum of the array is guaranteed to be non-negative ($\sum a_i \ge 0$), taking postponed debits out of the sequence guarantees that the remaining prefix balance stays non-negative, and processing the postponed debits at the end will also leave the final balance non-negative.
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$, where $N$ is the length of
a. Each element is pushed to the min-heap at most once and popped at most once. - Space Complexity: $\mathcal{O}(N)$ to store negative transactions in the min-heap.
import heapq
from typing import List
def min_expenses_to_move(a: List[int]) -> int:
balance = 0
debit_heap = []
postponed_count = 0
for val in a:
balance += val
if val < 0:
heapq.heappush(debit_heap, val)
if balance < 0:
# Reclaim balance by postponing the largest magnitude debit seen so far
largest_debit = heapq.heappop(debit_heap)
balance -= largest_debit
postponed_count += 1
return postponed_count