java.lang.Object
g2401_2500.s2412_minimum_money_required_before_transactions.Solution

public class Solution extends Object
2412 - Minimum Money Required Before Transactions.<p>Hard</p> <p>You are given a <strong>0-indexed</strong> 2D integer array <code>transactions</code>, where <code>transactions[i] = [cost<sub>i</sub>, cashback<sub>i</sub>]</code>.</p> <p>The array describes transactions, where each transaction must be completed exactly once in <strong>some order</strong>. At any given moment, you have a certain amount of <code>money</code>. In order to complete transaction <code>i</code>, <code>money >= cost<sub>i</sub></code> must hold true. After performing a transaction, <code>money</code> becomes <code>money - cost<sub>i</sub> + cashback<sub>i</sub></code>.</p> <p>Return <em>the minimum amount of</em> <code>money</code> <em>required before any transaction so that all of the transactions can be completed <strong>regardless of the order</strong> of the transactions.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> transactions = [[2,1],[5,0],[4,2]]</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong></p> <p>Starting with money = 10, the transactions can be performed in any order.</p> <p>It can be shown that starting with money < 10 will fail to complete all transactions in some order.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> transactions = [[3,0],[0,3]]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3.</p> </li> <li> <p>If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0.</p> </li> </ul> <p>Thus, starting with money = 3, the transactions can be performed in any order.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= transactions.length <= 10<sup>5</sup></code></li> <li><code>transactions[i].length == 2</code></li> <li><code>0 <= cost<sub>i</sub>, cashback<sub>i</sub> <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumMoney

      public long minimumMoney(int[][] transactions)