返回介绍

solution / 2400-2499 / 2412.Minimum Money Required Before Transactions / README_EN

发布于 2024-06-17 01:03:06 字数 4614 浏览 0 评论 0 收藏 0

2412. Minimum Money Required Before Transactions

中文文档

Description

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 <= 105
  • transactions[i].length == 2
  • 0 <= costi, cashbacki <= 109

Solutions

Solution 1: Greedy

First, we accumulate all the negative profits, denoted as $s$. Then we enumerate each transaction as the last transaction. If transactions[i].x > transactions[i].y, it means the current transaction is losing money, and this transaction has been calculated when we previously accumulated negative profits, so we update the answer with s + transactions[i].y; otherwise, we update the answer with s + transactions[i].x.

The time complexity is $O(n)$, where $n$ is the number of transactions. The space complexity is $O(1)$.

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
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;
  }
}
class Solution {
public:
  long long minimumMoney(vector<vector<int>>& transactions) {
    long long s = 0, ans = 0;
    for (auto& e : transactions) {
      s += max(0, e[0] - e[1]);
    }
    for (auto& e : transactions) {
      if (e[0] > e[1]) {
        ans = max(ans, s + e[1]);
      } else {
        ans = max(ans, s + e[0]);
      }
    }
    return ans;
  }
};
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)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文