2412. Minimum Money Required Before Transactions LeetCode Solution

In this guide, you will get 2412. Minimum Money Required Before Transactions LeetCode Solution with the best time and space complexity. The solution to Minimum Money Required Before Transactions problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Money Required Before Transactions solution in C++
  4. Minimum Money Required Before Transactions solution in Java
  5. Minimum Money Required Before Transactions solution in Python
  6. Additional Resources
2412. Minimum Money Required Before Transactions LeetCode Solution image

Problem Statement of Minimum Money Required Before Transactions

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.

See also  1535. Find the Winner of an Array Game LeetCode Solution

Constraints:

1 <= transactions.length <= 105
transactions[i].length == 2
0 <= costi, cashbacki <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2412. Minimum Money Required Before Transactions LeetCode Solution in C++

class Solution {
 public:
  long long minimumMoney(vector<vector<int>>& transactions) {
    long ans = 0;
    long losses = 0;

    // Before picking the final transaction, perform any transaction that raises
    // the required money.
    for (const vector<int>& t : transactions) {
      const int cost = t[0];
      const int cashback = t[1];
      losses += max(0, cost - cashback);
    }

    // Now, pick a transaction to be the final one.
    for (const vector<int>& t : transactions) {
      const int cost = t[0];
      const int cashback = t[1];
      if (cost > cashback)
        // The losses except this transaction: losses - (cost - cashback), so
        // add the cost of this transaction = losses - (cost - cashback) + cost.
        ans = max(ans, losses + cashback);
      else
        // The losses except this transaction: losses, so add the cost of this
        // transaction = losses + cost.
        ans = max(ans, losses + cost);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2412. Minimum Money Required Before Transactions LeetCode Solution in Java

class Solution {
  public long minimumMoney(int[][] transactions) {
    long ans = 0;
    long losses = 0;

    // Before picking the final transaction, perform any transaction that raises
    // the required money.
    for (int[] t : transactions) {
      final int cost = t[0];
      final int cashback = t[1];
      losses += Math.max(0, cost - cashback);
    }

    // Now, pick a transaction to be the final one.
    for (int[] t : transactions) {
      final int cost = t[0];
      final int cashback = t[1];
      if (cost > cashback)
        // The losses except this transaction: losses - (cost - cashback), so
        // add the cost of this transaction = losses - (cost - cashback) + cost.
        ans = Math.max(ans, losses + cashback);
      else
        // The losses except this transaction: losses, so add the cost of this
        // transaction = losses + cost.
        ans = Math.max(ans, losses + cost);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2412. Minimum Money Required Before Transactions LeetCode Solution in Python

class Solution:
  def minimumMoney(self, transactions: list[list[int]]) -> int:
    ans = 0
    losses = 0

    # Before picking the final transaction, perform any transaction that raises
    # the required money.
    for cost, cashback in transactions:
      losses += max(0, cost - cashback)

    # Now, pick a transaction to be the final one.
    for cost, cashback in transactions:
      if cost > cashback:
        # The losses except this transaction: losses - (cost - cashback), so
        # add the cost of this transaction = losses - (cost - cashback) + cost.
        ans = max(ans, losses + cashback)
      else:
        # The losses except this transaction: losses, so add the cost of this
        # transaction = losses + cost.
        ans = max(ans, losses + cost)

    return ans
# code by PROGIEZ

Additional Resources

See also  2108. Find First Palindromic String in the Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.