C++速通LeetCode简单第16题-买卖股票的最佳时机

思路要点:假设当天卖,动态更新最低价格和最大利益

cpp 复制代码
class Solution {
public:
    //要点:假设当天卖,动态更新最低价格和最大利益
    int maxProfit(vector<int>& prices) {
        int ans = 0;
        int lowest = prices[0];
        for(int i = 1; i < prices.size(); i++)//假设当天卖
        {
           lowest = min(lowest,prices[i]);//记录当天之前的最低价
           ans = max(ans,prices[i] - lowest);//记录当天卖的最大利益
        }
        if(ans > 0) return ans;
        else return 0;
    }
};
相关推荐
低调小一4 小时前
Fresco 图片加载全链路解析:从 SimpleDraweeView 到 Producer 责任链
android·开发语言·fresco
_周游4 小时前
Java8 API文档搜索引擎_7.项目优化之权重合并
java·开发语言·前端·搜索引擎·intellij-idea
专注VB编程开发20年4 小时前
c#.NET异步同小,ASYNC,AWAIT,PushFrame ,DOEVENTS
开发语言·.net
电商API_180079052475 小时前
淘宝商品详情数据获取全方案分享
开发语言·前端·javascript
blackicexs5 小时前
第四周第四天
数据结构·c++·算法
TracyCoder1235 小时前
LeetCode Hot100(46/100)——74. 搜索二维矩阵
算法·leetcode·矩阵
im_AMBER5 小时前
Leetcode 119 二叉树展开为链表 | 路径总和
数据结构·学习·算法·leetcode·二叉树
知无不研5 小时前
c++的设计模式(常用)
c++·观察者模式·单例模式·设计模式·简单工厂模式
fpcc5 小时前
并行编程实战——CUDA编程的并行前缀和
c++·cuda
maplewen.5 小时前
C++11 返回值优化
开发语言·c++·面试