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. python 爬虫新手入门教程

    python 爬虫新手教程 一.什么是爬虫 爬虫就是把一个网站里的内容读取下来 这里我们就要学习一个知识 我们看到的网页是有一种叫HTML的语言编写的 他可以给文字显示不同的样式 如:<p> ...

  2. PHP中的那些魔术常量

    之前我们已经了解了一些常用的魔术方法,除了魔术方法外,PHP还提供一些魔术常量,相信大家在日常的工作中也都使用过,这里给大家做一个总结. 其实PHP还提供了很多常量但都依赖于各类扩展库,而有几个常量是 ...

  3. Nginx系列(7)- Nginx安装 | Linux

    step-1 安装gcc 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: [root@localhost ~]# yum install ...

  4. Struts2 S2-061 远程命令执行漏洞复现(CVE-2020-17530)

    0x01 漏洞简介 Struts在某些情况下可能存在OGNL表达式注入漏洞,如果开发人员使用了 %{-} 语法进行强制OGNL解析,某些特殊的TAG属性可能会被双重解析.攻击者可以通过构造恶意的OGN ...

  5. httprunner版本没有更新问题

    使用命令行创建虚拟环境,创建脚手架目录后,使用pycharm打开所创建的脚手架目录. 执行:hrun demo_testcase_request.yml 提示: E:\hrun_ven\test_hr ...

  6. 使用亚马逊服务器报错:Signature not yet current: 20190726T070253Z is still later than 20190726T070246Z (20190726T065746Z + 15 min.)时间不同步的解决办法

    1.首先获取亚马逊的时间: $ curl http://s3.amazonaws.com -v 2.更改当前服务器时间,使之与亚马逊时间同步 $ date -s 'xxxx-xx-xx xx:xx:x ...

  7. 鸿蒙内核源码分析(编译过程篇) | 简单案例窥视GCC编译全过程 | 百篇博客分析OpenHarmony源码| v57.01

    百篇博客系列篇.本篇为: v57.xx 鸿蒙内核源码分析(编译过程篇) | 简单案例窥视编译全过程 | 51.c.h.o 编译构建相关篇为: v50.xx 鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙 ...

  8. AT5661-[AGC040C]Neither AB nor BA【模型转换】

    正题 题目链接:https://www.luogu.com.cn/problem/AT5661 题目大意 一个包含\(A,B,C\)的序列,每次可以选择相邻的两个除了\(AB\)和\(BA\)的删去. ...

  9. P5607-[Ynoi2013]无力回天NOI2017【线性基,线段树,树状数组】

    正题 题目链接:https://www.luogu.com.cn/problem/P5607 题目大意 \(n\)个数字的序列,\(m\)次操作 区间\([l,r]\)异或上一个值\(v\) 询问区间 ...

  10. 前端规范之CSS规范(Stylelint)

    代码规范是软件开发领域经久不衰的话题,几乎所有工程师在开发过程中都会遇到或思考过这一问题.而随着前端应用的大型化和复杂化,越来越多的前端团队也开始重视代码规范.同样,前段时间,笔者所在的团队也开展了一 ...