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;
    }
};
相关推荐
longlongzihan2 小时前
从LeetCode 70. 爬楼梯看动态规划:如何精准推导状态转移方程?
c++·算法·leetcode·动态规划
Rnan-prince7 小时前
LeetCode 股票买卖系列:一个动态规划状态机模板通吃四题
leetcode·动态规划
圣保罗的大教堂8 小时前
leetcode 3756. 连接非零数字并乘以其数字和 II 中等
leetcode
All for pursuit.12 小时前
【回溯-7】494.目标和
数据结构·c++·算法·leetcode
Tisfy13 小时前
LeetCode 0678.有效的括号字符串:O(n)+O(1)一次遍历
算法·leetcode·字符串·题解·贪心·括号匹配
Navigator_Z1 天前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
青山木1 天前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
All for pursuit.1 天前
【回溯-4】46.全排列
数据结构·c++·算法·leetcode
All for pursuit.1 天前
【回溯-5】78.子集
数据结构·c++·算法·leetcode
代码什么用2 天前
LeetCode-560.和为 K 的子数组
算法·leetcode