Say you have an array for which the i th element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

C++

class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size() < 2) return 0;
int maxPro = 0;
int curMin = prices[0];
for(int i =1; i < prices.size();i++){
if (prices[i] < curMin)
curMin = prices[i];
else
maxPro = max(prices[i] - curMin,maxPro);
}
return maxPro;
} int maxProfit4(vector<int> &prices) {
if(prices.size() < 2) return 0;
int maxPro = 0;
int curMin = prices[0];
for(int i =1; i < prices.size();i++){
int cur = prices[i];
if (cur < curMin)
curMin = cur;
else{
int curPro = cur - curMin;
if (curPro > maxPro)
maxPro = curPro;
}
}
return maxPro;
} int maxProfit2(vector<int> &prices) {
if(prices.size() < 2) return 0;
int maxPro = 0;
int curMin = prices[0];
for(int i =1; i < prices.size();i++){
if (prices[i] < curMin)
curMin = prices[i];
else
if (prices[i] - curMin > maxPro)
maxPro = prices[i] - curMin;
}
return maxPro;
}
};

best-time-to-buy-and-sell-stock leetcode C++的更多相关文章

  1. Best Time to Buy and Sell Stock - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...

  2. Best Time to Buy and Sell Stock——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. Best Time to Buy and Sell Stock leetcode java

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  4. 121. Best Time to Buy and Sell Stock——Leetcode

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  5. 最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode

    LeetCode 我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益. 我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前 ...

  6. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

随机推荐

  1. 用java代码遍历excel文件并回显

    今天需要完成282个指标,分析后发现好多都是可复用的字段和方法,生成的dao类也是很多重复的代码,所以写下了简单的自动化遍历excel的test方法, excel业务逻辑如下,用了 HSSFSheet ...

  2. PHP密码散列算法的学习

    不知道大家有没有看过 Laravel 的源码.在 Laravel 源码中,对于用户密码的加密,使用的是 password_hash() 这个函数.这个函数是属于 PHP 密码散列算法扩展中所包含的函数 ...

  3. Nginx系列(6)- nginx: [error] CreateFile() "D:\nginx-1.20.1/logs/nginx.pid" failed (2: The system cannot find the file specified)

    背景 修改nginx配置文件nginx.conf后,想要重启nginx使配置生效.cmd进入nginx安装目录,输入命令: nginx -s reload 报错:nginx: [error] Crea ...

  4. Fiddler抓包(以谷歌浏览器、安卓手机为例)

    fiddler抓包流程与whistle相同,所以本章内容会相对简洁.如果需要详细说明,可参考whistle抓包. 这里以谷歌浏览器.安卓手机为例. 1.fiddler安装 下载安装包,默认安装. 2. ...

  5. c++ 的学习 第二集函数的重载2 namemangling

    1. 本质 采用了name mangling或者叫name decoration ✓ C++编译器默认会对符号名(比如函数名)进行改编.修饰,有些地方翻译为"命名倾轧"✓ 重载时 ...

  6. 借助Cookie实现是否第一次登陆/显示上次登陆时间

    Cookie实现是否第一次登陆/显示上次登陆时间 最近刚好看到Cookie这方面知识,对Servlet部分知识已经生疏,重新翻出已经遗弃角落的<JavaWeb开发实战经典>,重新温习了Co ...

  7. Centos7 配置JDK 提示 /lib/ld-linux.so.2: bad ELF interpreter: No such file or direct

    解决办法:yum install glibc.i686

  8. 对于caffe程序中出现的Unknown database backend问题的报错怎么办?

    在预处理器中添加USE_LMDB,因为caffe需要一种数据输入格式 这样,在db.cpp中#ifdef USE_LMDB就会变亮,显示使用的数据格式为LMDB

  9. 国内首篇云厂商 Serverless 论文入选全球顶会:突发流量下,如何加速容器启动?

    作者 | 王骜 来源 | Serverless 公众号 导读 ​ USENIX ATC (USENIX Annual Technical Conference) 学术会议是计算机系统领域的顶级会议,入 ...

  10. Apache ShardingSphere:由开源驱动的分布式数据库中间件生态

    2021 年 7 月 21 日 2021 亚马逊云科技中国峰会现场,SphereEx 联合创始人.Apache ShardingSphere PMC 潘娟受邀参与此次峰会,以<Apache Sh ...