leetcode 2412. 完成所有交易的初始最少钱数

题目:2412. 完成所有交易的初始最少钱数 - 力扣(LeetCode)

题目要求的就是最差情况的本钱最少是多少,肯定是先亏钱再赚钱。

对于每个交易:

  • 如果是赚钱或保本,只有最大的本金对本钱有影响
  • 如果是亏钱,所有交易都影响本金
  • 如果全部交易都是亏钱的,最差情况下,最后一次交易的盈利应当最大

基于这些条件,可以简化计算方式:

复制代码
class Solution {
public:
    long long minimumMoney(vector<vector<int>>& transactions) {
        long long ret = 0;
        int lc = 0;
        for (int i = 0; i < transactions.size(); i++) {
            vector<int>& t = transactions[i];
            if (t[0] > t[1]) {
                ret += t[0] - t[1];
            }
            if (t[0] > lc && t[1] > lc) {
                lc = min(t[0], t[1]);
            }
        }
        ret += lc;
        return ret;
    }
};
相关推荐
穿条秋裤到处跑3 小时前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
水蓝烟雨9 小时前
1931. 用三种不同颜色为网格涂色
算法·leetcode
leoufung11 小时前
LeetCode 76:Minimum Window Substring 题解与滑动窗口思维详解
算法·leetcode·职场和发展
风筝在晴天搁浅12 小时前
LeetCode 92.反转链表Ⅱ
算法·leetcode·链表
普贤莲花16 小时前
【2026年第18周---写于20260501】---舍得
程序人生·算法·leetcode
m0_6294947316 小时前
LeetCode 热题 100-----16.除了自身以外数组的乘积
数据结构·算法·leetcode
We་ct17 小时前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·leetcode·typescript·动态规划
无敌昊哥战神17 小时前
【LeetCode 37】解数独 (Sudoku Solver) —— 回溯法详解 (Python/C/C++)
c语言·c++·python·算法·leetcode
风筝在晴天搁浅17 小时前
LeetCode 162.寻找峰值
算法·leetcode
罗超驿18 小时前
双指针算法经典案例:LeetCode 283. 移动零(Java详解)
java·算法·leetcode