Skip to content

Commit 45d130e

Browse files
committed
+ problem 2412
1 parent c684c42 commit 45d130e

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 2412. Minimum Money Required Before Transactions
2+
You are given a **0-indexed** 2D integer array `transactions`, where <code>transactions[i] = [cost<sub>i</sub>, cashback<sub>i</sub>]</code>.
3+
4+
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`, <code>money >= cost<sub>i</sub></code> must hold true. After performing a transaction, `money` becomes <code>money - cost<sub>i</sub> + cashback<sub>i</sub></code>.
5+
6+
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*.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> transactions = [[2,1],[5,0],[4,2]]
11+
<strong>Output:</strong> 10
12+
<strong>Explanation:</strong>
13+
Starting with money = 10, the transactions can be performed in any order.
14+
It can be shown that starting with money < 10 will fail to complete all transactions in some order.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> transactions = [[3,0],[0,3]]
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong>
22+
- If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3.
23+
- If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0.
24+
Thus, starting with money = 3, the transactions can be performed in any order.
25+
</pre>
26+
27+
#### Constraints:
28+
* <code>1 <= transactions.length <= 10<sup>5</sup></code>
29+
* `transactions[i].length == 2`
30+
* <code>0 <= cost<sub>i</sub>, cashback<sub>i</sub> <= 10<sup>9</sup></code>
31+
32+
## Solutions (Rust)
33+
34+
### 1. Solution
35+
```Rust
36+
impl Solution {
37+
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
38+
let mut loss = 0;
39+
let mut max_cost = 0;
40+
41+
for t in &transactions {
42+
loss += (t[0] - t[1]).max(0) as i64;
43+
max_cost = max_cost.max(t[0] - (t[0] - t[1]).max(0));
44+
}
45+
46+
max_cost as i64 + loss
47+
}
48+
}
49+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 2412. 完成所有交易的初始最少钱数
2+
给你一个下标从 **0** 开始的二维整数数组 `transactions`,其中<code>transactions[i] = [cost<sub>i</sub>, cashback<sub>i</sub>]</code> 。
3+
4+
数组描述了若干笔交易。其中每笔交易必须以 **某种顺序** 恰好完成一次。在任意一个时刻,你有一定数目的钱 `money` ,为了完成交易 `i` ,<code>money >= cost<sub>i</sub></code> 这个条件必须为真。执行交易后,你的钱数 `money` 变成 <code>money - cost<sub>i</sub> + cashback<sub>i</sub></code> 。
5+
6+
请你返回 **任意一种** 交易顺序下,你都能完成所有交易的最少钱数 `money` 是多少。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> transactions = [[2,1],[5,0],[4,2]]
11+
<strong>输出:</strong> 10
12+
<strong>解释:</strong>
13+
刚开始 money = 10 ,交易可以以任意顺序进行。
14+
可以证明如果 money < 10 ,那么某些交易无法进行。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> transactions = [[3,0],[0,3]]
20+
<strong>输出:</strong> 3
21+
<strong>解释:</strong>
22+
- 如果交易执行的顺序是 [[3,0],[0,3]] ,完成所有交易需要的最少钱数是 3 。
23+
- 如果交易执行的顺序是 [[0,3],[3,0]] ,完成所有交易需要的最少钱数是 0 。
24+
所以,刚开始钱数为 3 ,任意顺序下交易都可以全部完成。
25+
</pre>
26+
27+
#### 提示:
28+
* <code>1 <= transactions.length <= 10<sup>5</sup></code>
29+
* `transactions[i].length == 2`
30+
* <code>0 <= cost<sub>i</sub>, cashback<sub>i</sub> <= 10<sup>9</sup></code>
31+
32+
## 题解 (Rust)
33+
34+
### 1. 题解
35+
```Rust
36+
impl Solution {
37+
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
38+
let mut loss = 0;
39+
let mut max_cost = 0;
40+
41+
for t in &transactions {
42+
loss += (t[0] - t[1]).max(0) as i64;
43+
max_cost = max_cost.max(t[0] - (t[0] - t[1]).max(0));
44+
}
45+
46+
max_cost as i64 + loss
47+
}
48+
}
49+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
3+
let mut loss = 0;
4+
let mut max_cost = 0;
5+
6+
for t in &transactions {
7+
loss += (t[0] - t[1]).max(0) as i64;
8+
max_cost = max_cost.max(t[0] - (t[0] - t[1]).max(0));
9+
}
10+
11+
max_cost as i64 + loss
12+
}
13+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,7 @@
13401340
[2405][2405l]|[Optimal Partition of String][2405] |![rs]
13411341
[2409][2409l]|[Count Days Spent Together][2409] |![py]
13421342
[2410][2410l]|[Maximum Matching of Players With Trainers][2410] |![rs]
1343+
[2412][2412l]|[Minimum Money Required Before Transactions][2412] |![rs]
13431344
[2413][2413l]|[Smallest Even Multiple][2413] |![rs]
13441345
[2414][2414l]|[Length of the Longest Alphabetical Continuous Substring][2414] |![rs]
13451346
[2415][2415l]|[Reverse Odd Levels of Binary Tree][2415] |![py]
@@ -2766,6 +2767,7 @@
27662767
[2405]:Problemset/2405-Optimal%20Partition%20of%20String/README.md#2405-optimal-partition-of-string
27672768
[2409]:Problemset/2409-Count%20Days%20Spent%20Together/README.md#2409-count-days-spent-together
27682769
[2410]:Problemset/2410-Maximum%20Matching%20of%20Players%20With%20Trainers/README.md#2410-maximum-matching-of-players-with-trainers
2770+
[2412]:Problemset/2412-Minimum%20Money%20Required%20Before%20Transactions/README.md#2412-minimum-money-required-before-transactions
27692771
[2413]:Problemset/2413-Smallest%20Even%20Multiple/README.md#2413-smallest-even-multiple
27702772
[2414]:Problemset/2414-Length%20of%20the%20Longest%20Alphabetical%20Continuous%20Substring/README.md#2414-length-of-the-longest-alphabetical-continuous-substring
27712773
[2415]:Problemset/2415-Reverse%20Odd%20Levels%20of%20Binary%20Tree/README.md#2415-reverse-odd-levels-of-binary-tree
@@ -4191,6 +4193,7 @@
41914193
[2405l]:https://leetcode.com/problems/optimal-partition-of-string/
41924194
[2409l]:https://leetcode.com/problems/count-days-spent-together/
41934195
[2410l]:https://leetcode.com/problems/maximum-matching-of-players-with-trainers/
4196+
[2412l]:https://leetcode.com/problems/minimum-money-required-before-transactions/
41944197
[2413l]:https://leetcode.com/problems/smallest-even-multiple/
41954198
[2414l]:https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/
41964199
[2415l]:https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,7 @@
13401340
[2405][2405l]|[子字符串的最优划分][2405] |![rs]
13411341
[2409][2409l]|[统计共同度过的日子数][2409] |![py]
13421342
[2410][2410l]|[运动员和训练师的最大匹配数][2410] |![rs]
1343+
[2412][2412l]|[完成所有交易的初始最少钱数][2412] |![rs]
13431344
[2413][2413l]|[最小偶倍数][2413] |![rs]
13441345
[2414][2414l]|[最长的字母序连续子字符串的长度][2414] |![rs]
13451346
[2415][2415l]|[反转二叉树的奇数层][2415] |![py]
@@ -2766,6 +2767,7 @@
27662767
[2405]:Problemset/2405-Optimal%20Partition%20of%20String/README_CN.md#2405-子字符串的最优划分
27672768
[2409]:Problemset/2409-Count%20Days%20Spent%20Together/README_CN.md#2409-统计共同度过的日子数
27682769
[2410]:Problemset/2410-Maximum%20Matching%20of%20Players%20With%20Trainers/README_CN.md#2410-运动员和训练师的最大匹配数
2770+
[2412]:Problemset/2412-Minimum%20Money%20Required%20Before%20Transactions/README_CN.md#2412-完成所有交易的初始最少钱数
27692771
[2413]:Problemset/2413-Smallest%20Even%20Multiple/README_CN.md#2413-最小偶倍数
27702772
[2414]:Problemset/2414-Length%20of%20the%20Longest%20Alphabetical%20Continuous%20Substring/README_CN.md#2414-最长的字母序连续子字符串的长度
27712773
[2415]:Problemset/2415-Reverse%20Odd%20Levels%20of%20Binary%20Tree/README_CN.md#2415-反转二叉树的奇数层
@@ -4191,6 +4193,7 @@
41914193
[2405l]:https://leetcode.cn/problems/optimal-partition-of-string/
41924194
[2409l]:https://leetcode.cn/problems/count-days-spent-together/
41934195
[2410l]:https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/
4196+
[2412l]:https://leetcode.cn/problems/minimum-money-required-before-transactions/
41944197
[2413l]:https://leetcode.cn/problems/smallest-even-multiple/
41954198
[2414l]:https://leetcode.cn/problems/length-of-the-longest-alphabetical-continuous-substring/
41964199
[2415l]:https://leetcode.cn/problems/reverse-odd-levels-of-binary-tree/

0 commit comments

Comments
 (0)