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;
    }
};
相关推荐
文丑颜不良啊15 分钟前
LeetCode876~900题目解析
java·leetcode
木井巳9 小时前
【动态规划: 路径问题】不同路径
java·算法·leetcode·动态规划
晒太羊的猫10 小时前
hot100子串
java·算法·leetcode
海清河晏11111 小时前
加油站问题
数据结构·算法·leetcode
Nuanyt12 小时前
LeetCode Hot 100 · 双指针 3 题 Java
java·笔记·算法·leetcode·双指针
文丑颜不良啊14 小时前
LeetCode851~875题目解析
java·leetcode
Navigator_Z14 小时前
LeetCode //C - 1304. Find N Unique Integers Sum up to Zero
c语言·算法·leetcode
?abc!1 天前
(数组)581. 最短无序连续子数组
数据结构·算法·leetcode
hold?fish:palm1 天前
40 二叉树的直径
c++·leetcode
guo_wen_qiang1 天前
二叉树的层序遍历-使用双端队列Deque
算法·leetcode·1024程序员节