Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a 0-indexed 2D integer array transactions, where transactions[i] = [costi, cashbacki].
The array describes transactions, where each transaction must be completed exactly once in some order. At any given moment, you have a certain amount of money. In order to complete transaction i, money >= costi must hold true. After performing a transaction, money becomes money - costi + cashbacki.
Return the minimum amount of money required before any transaction so that all of the transactions can be completed regardless of the order of the transactions.
Example 1:
Input: transactions = [[2,1],[5,0],[4,2]] Output: 10 Explanation: Starting with money = 10, the transactions can be performed in any order. It can be shown that starting with money < 10 will fail to complete all transactions in some order.
Example 2:
Input: transactions = [[3,0],[0,3]] Output: 3 Explanation: - If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3. - If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0. Thus, starting with money = 3, the transactions can be performed in any order.
Constraints:
1 <= transactions.length <= 105transactions[i].length == 20 <= costi, cashbacki <= 109Problem summary: You are given a 0-indexed 2D integer array transactions, where transactions[i] = [costi, cashbacki]. The array describes transactions, where each transaction must be completed exactly once in some order. At any given moment, you have a certain amount of money. In order to complete transaction i, money >= costi must hold true. After performing a transaction, money becomes money - costi + cashbacki. Return the minimum amount of money required before any transaction so that all of the transactions can be completed regardless of the order of the transactions.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[[2,1],[5,0],[4,2]]
[[3,0],[0,3]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2412: Minimum Money Required Before Transactions
class Solution {
public long minimumMoney(int[][] transactions) {
long s = 0;
for (var e : transactions) {
s += Math.max(0, e[0] - e[1]);
}
long ans = 0;
for (var e : transactions) {
if (e[0] > e[1]) {
ans = Math.max(ans, s + e[1]);
} else {
ans = Math.max(ans, s + e[0]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2412: Minimum Money Required Before Transactions
func minimumMoney(transactions [][]int) int64 {
s, ans := 0, 0
for _, e := range transactions {
s += max(0, e[0]-e[1])
}
for _, e := range transactions {
if e[0] > e[1] {
ans = max(ans, s+e[1])
} else {
ans = max(ans, s+e[0])
}
}
return int64(ans)
}
# Accepted solution for LeetCode #2412: Minimum Money Required Before Transactions
class Solution:
def minimumMoney(self, transactions: List[List[int]]) -> int:
s = sum(max(0, a - b) for a, b in transactions)
ans = 0
for a, b in transactions:
if a > b:
ans = max(ans, s + b)
else:
ans = max(ans, s + a)
return ans
// Accepted solution for LeetCode #2412: Minimum Money Required Before Transactions
impl Solution {
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
let mut s: i64 = 0;
for transaction in &transactions {
let (a, b) = (transaction[0], transaction[1]);
s += (a - b).max(0) as i64;
}
let mut ans = 0;
for transaction in &transactions {
let (a, b) = (transaction[0], transaction[1]);
if a > b {
ans = ans.max(s + b as i64);
} else {
ans = ans.max(s + a as i64);
}
}
ans
}
}
// Accepted solution for LeetCode #2412: Minimum Money Required Before Transactions
function minimumMoney(transactions: number[][]): number {
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
let ans = 0;
for (const [a, b] of transactions) {
if (a > b) {
ans = Math.max(ans, s + b);
} else {
ans = Math.max(ans, s + a);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.