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;
    }
};
相关推荐
共享家95274 小时前
经典动态规划题解
算法·leetcode·动态规划
1白天的黑夜16 小时前
栈-844.比较含退格的字符串-力扣(LeetCode)
c++·leetcode·
Swift社区8 小时前
LeetCode 378 - 有序矩阵中第 K 小的元素
算法·leetcode·矩阵
墨染点香8 小时前
LeetCode 刷题【73. 矩阵置零】
算法·leetcode·矩阵
林木辛8 小时前
LeetCode热题 438.找到字符中所有字母异位词 (滑动窗口)
算法·leetcode
dragoooon349 小时前
[优选算法专题二——NO.16最小覆盖子串]
c++·算法·leetcode·学习方法
1白天的黑夜112 小时前
栈-1047.删除字符串中的所有相邻重复项-力扣(LeetCode)
c++·leetcode·
im_AMBER12 小时前
Leetcode 18 java
java·算法·leetcode
愚润求学13 小时前
【贪心算法】day9
c++·算法·leetcode·贪心算法