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;
    }
};
相关推荐
XFF不秃头2 小时前
力扣刷题笔记-全排列
c++·笔记·算法·leetcode
菜鸟233号2 小时前
力扣669 修剪二叉搜索树 java实现
java·数据结构·算法·leetcode
yaoh.wang3 小时前
力扣(LeetCode) 100: 相同的树 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
SadSunset3 小时前
力扣题目142. 环形链表 II的解法分享,附图解
算法·leetcode·链表
iAkuya4 小时前
(leetcode)力扣100 19螺旋矩阵(方向数组/边界把控)
算法·leetcode·矩阵
爱编程的小吴4 小时前
【力扣练习题】热题100道【哈希】 最长连续序列
算法·leetcode·职场和发展
bybitq6 小时前
Leetcode-3780-Python
python·算法·leetcode
如何原谅奋力过但无声6 小时前
【力扣-Python-75】颜色分类(middle)
python·算法·leetcode
玖剹6 小时前
哈希表相关题目
数据结构·c++·算法·leetcode·哈希算法·散列表
练习时长一年7 小时前
LeetCode热题100(最小栈)
java·算法·leetcode