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;
    }
};
相关推荐
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 141. 环形链表 | C++ 哈希表直觉解法
c++·leetcode·链表
北顾笙9808 小时前
day35-数据结构力扣
数据结构·算法·leetcode
ulias21210 小时前
leetcode热题 - 4
算法·leetcode·职场和发展
圣保罗的大教堂10 小时前
leetcode 1559. 二维网格图中探测环 中等
leetcode
_日拱一卒11 小时前
LeetCode:148排序链表
算法·leetcode·链表
生信研究猿11 小时前
leetcode 78.子集
算法·leetcode·深度优先
浅念-11 小时前
分治算法专题|LeetCode高频经典题目详细题解
数据结构·c++·算法·leetcode·职场和发展·排序·分治
shehuiyuelaiyuehao12 小时前
算法11,滑动窗口,最大连续1的个数|||
算法·leetcode·职场和发展
脱氧核糖核酸__12 小时前
LeetCode热题100——206.反转链表(迭代法)
c++·leetcode·链表
北顾笙98013 小时前
day34-数据结构力扣
数据结构·算法·leetcode