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;
    }
};
相关推荐
故事和你913 小时前
洛谷-算法1-7-搜索3
数据结构·c++·算法·leetcode·动态规划
y = xⁿ6 小时前
20天速通LeetCode day07:前缀和
数据结构·算法·leetcode
小雅痞6 小时前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode
北顾笙9808 小时前
day26-数据结构力扣
数据结构·算法·leetcode
故事和你918 小时前
洛谷-数据结构1-2-二叉树1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
_日拱一卒9 小时前
LeetCode:19删除链表的倒数第N个节点
算法·leetcode·链表
y = xⁿ9 小时前
20天速通LeetCode day08:关于栈
算法·leetcode·职场和发展
XWalnut9 小时前
LeetCode刷题 day13
数据结构·算法·leetcode
im_AMBER11 小时前
Leetcode 158 数组中的第K个最大元素 | 查找和最小的 K 对数字
javascript·数据结构·算法·leetcode·
脱氧核糖核酸__11 小时前
LeetCode热题100——48.旋转图像(题解+答案+要点)
c++·算法·leetcode