Skip to content

2412. Minimum Money Required Before Transactions 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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