Skip to content

Commit d7dfeb5

Browse files
authoredJan 24, 2025··
feat: add solutions to lc problem: No.2412 (#3993)
No.2412.Minimum Money Required Before Transactions
1 parent eae0c1f commit d7dfeb5

File tree

5 files changed

+173
-2
lines changed

5 files changed

+173
-2
lines changed
 

‎solution/2400-2499/2412.Minimum Money Required Before Transactions/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ tags:
6767

6868
### 方法一:贪心
6969

70-
我们先累计所有负收益,记为 $s$。然后枚举每个交易作为最后一个交易,如果 `transactions[i].x > transactions[i].y`说明当前的交易是亏钱的,而这个交易在此前我们累计负收益的时候,已经被计算,因此取 `s + transactions[i].y` 更新答案;否则,取 `s + transactions[i].x` 更新答案。
70+
我们先累计所有负收益,记为 $s$。然后枚举每个交易 $\text{transactions}[i] = [a, b]$ 作为最后一个交易,如果 $a > b$,说明当前的交易是亏钱的,而这个交易在此前我们累计负收益的时候,已经被计算,因此取 $s + b$ 更新答案;否则,取 $s + a$ 更新答案。
7171

7272
时间复杂度 $O(n)$,其中 $n$ 为交易数。空间复杂度 $O(1)$。
7373

@@ -151,6 +151,68 @@ func minimumMoney(transactions [][]int) int64 {
151151
}
152152
```
153153

154+
#### TypeScript
155+
156+
```ts
157+
function minimumMoney(transactions: number[][]): number {
158+
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
159+
let ans = 0;
160+
for (const [a, b] of transactions) {
161+
if (a > b) {
162+
ans = Math.max(ans, s + b);
163+
} else {
164+
ans = Math.max(ans, s + a);
165+
}
166+
}
167+
return ans;
168+
}
169+
```
170+
171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
176+
let mut s: i64 = 0;
177+
for transaction in &transactions {
178+
let (a, b) = (transaction[0], transaction[1]);
179+
s += (a - b).max(0) as i64;
180+
}
181+
let mut ans = 0;
182+
for transaction in &transactions {
183+
let (a, b) = (transaction[0], transaction[1]);
184+
if a > b {
185+
ans = ans.max(s + b as i64);
186+
} else {
187+
ans = ans.max(s + a as i64);
188+
}
189+
}
190+
ans
191+
}
192+
}
193+
```
194+
195+
#### JavaScript
196+
197+
```js
198+
/**
199+
* @param {number[][]} transactions
200+
* @return {number}
201+
*/
202+
var minimumMoney = function (transactions) {
203+
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
204+
let ans = 0;
205+
for (const [a, b] of transactions) {
206+
if (a > b) {
207+
ans = Math.max(ans, s + b);
208+
} else {
209+
ans = Math.max(ans, s + a);
210+
}
211+
}
212+
return ans;
213+
};
214+
```
215+
154216
<!-- tabs:end -->
155217

156218
<!-- solution:end -->

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

+63-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Thus, starting with money = 3, the transactions can be performed in any order.
6565

6666
### Solution 1: Greedy
6767

68-
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`.
68+
First, we accumulate all negative profits, denoted as $s$. Then, we enumerate each transaction $\text{transactions}[i] = [a, b]$ as the last transaction. If $a > b$, it means the current transaction is a loss, and this transaction has already been included when we accumulated the negative profits earlier. Therefore, we update the answer with $s + b$. Otherwise, we update the answer with $s + a$.
6969

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

@@ -149,6 +149,68 @@ func minimumMoney(transactions [][]int) int64 {
149149
}
150150
```
151151

152+
#### TypeScript
153+
154+
```ts
155+
function minimumMoney(transactions: number[][]): number {
156+
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
157+
let ans = 0;
158+
for (const [a, b] of transactions) {
159+
if (a > b) {
160+
ans = Math.max(ans, s + b);
161+
} else {
162+
ans = Math.max(ans, s + a);
163+
}
164+
}
165+
return ans;
166+
}
167+
```
168+
169+
#### Rust
170+
171+
```rust
172+
impl Solution {
173+
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
174+
let mut s: i64 = 0;
175+
for transaction in &transactions {
176+
let (a, b) = (transaction[0], transaction[1]);
177+
s += (a - b).max(0) as i64;
178+
}
179+
let mut ans = 0;
180+
for transaction in &transactions {
181+
let (a, b) = (transaction[0], transaction[1]);
182+
if a > b {
183+
ans = ans.max(s + b as i64);
184+
} else {
185+
ans = ans.max(s + a as i64);
186+
}
187+
}
188+
ans
189+
}
190+
}
191+
```
192+
193+
#### JavaScript
194+
195+
```js
196+
/**
197+
* @param {number[][]} transactions
198+
* @return {number}
199+
*/
200+
var minimumMoney = function (transactions) {
201+
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
202+
let ans = 0;
203+
for (const [a, b] of transactions) {
204+
if (a > b) {
205+
ans = Math.max(ans, s + b);
206+
} else {
207+
ans = Math.max(ans, s + a);
208+
}
209+
}
210+
return ans;
211+
};
212+
```
213+
152214
<!-- tabs:end -->
153215

154216
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[][]} transactions
3+
* @return {number}
4+
*/
5+
var minimumMoney = function (transactions) {
6+
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
7+
let ans = 0;
8+
for (const [a, b] of transactions) {
9+
if (a > b) {
10+
ans = Math.max(ans, s + b);
11+
} else {
12+
ans = Math.max(ans, s + a);
13+
}
14+
}
15+
return ans;
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
3+
let mut s: i64 = 0;
4+
for transaction in &transactions {
5+
let (a, b) = (transaction[0], transaction[1]);
6+
s += (a - b).max(0) as i64;
7+
}
8+
let mut ans = 0;
9+
for transaction in &transactions {
10+
let (a, b) = (transaction[0], transaction[1]);
11+
if a > b {
12+
ans = ans.max(s + b as i64);
13+
} else {
14+
ans = ans.max(s + a as i64);
15+
}
16+
}
17+
ans
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minimumMoney(transactions: number[][]): number {
2+
const s = transactions.reduce((acc, [a, b]) => acc + Math.max(0, a - b), 0);
3+
let ans = 0;
4+
for (const [a, b] of transactions) {
5+
if (a > b) {
6+
ans = Math.max(ans, s + b);
7+
} else {
8+
ans = Math.max(ans, s + a);
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.