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;
    }
};
相关推荐
Nil2087 小时前
leetcode 206反转链表
算法·leetcode·链表
想吃火锅100519 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
Nil20820 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
evans在进步1 天前
LeetCode 394:字符串解码——Java 单栈模拟与嵌套解析详解
java·python·leetcode
Nil2081 天前
leetcode 189轮转数组
数据结构·算法·leetcode
吃着火锅x唱着歌1 天前
LeetCode 3885.设计事件管理器
算法·leetcode·职场和发展
土司大王1 天前
LeetCode hto100——字母异位词分组
java·算法·leetcode
土司大王1 天前
LeetCode hot100——两数之和
数据结构·算法·leetcode
.道阻且长.1 天前
7.LeetCode算法习题讲解--双指针--四数之和
算法·leetcode·职场和发展
Navigator_Z1 天前
LeetCode //C - 1200. Minimum Absolute Difference
c语言·算法·leetcode