Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d82c53e

Browse files
Create 2412.Minimum-Money-Required-Before-Transactions.cpp
1 parent d8b7073 commit d82c53e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using LL = long long;
2+
class Solution {
3+
public:
4+
long long minimumMoney(vector<vector<int>>& transactions)
5+
{
6+
7+
sort(transactions.begin(), transactions.end(), [](vector<int>&a, vector<int>&b){return a[1]<b[1];});
8+
LL maxCostForNetGain = 0;
9+
10+
LL cur = 0;
11+
LL histLow = 0;
12+
13+
for (auto& trans: transactions)
14+
{
15+
LL cost = trans[0], back = trans[1];
16+
if (cost <= back)
17+
{
18+
maxCostForNetGain = max(maxCostForNetGain, cost);
19+
}
20+
else
21+
{
22+
cur -= cost;
23+
histLow = min(histLow, cur);
24+
cur += back;
25+
}
26+
}
27+
28+
cur += abs(histLow);
29+
30+
return abs(histLow) + max(0LL, maxCostForNetGain - cur);
31+
}
32+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /