题目链接

Best Time to Buy and Sell Stock - LeetCode

注意点

  • 在卖出之前必须要先购入

解法

解法一:遍历一遍,随时记录当前数字之前的最小的数字。将当前数字与当前最小数字相减查看收益。时间复杂度O(n)

class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
if(size < 1) return 0;
int max = 0,min = prices[0];
for(int i = 1;i < size;i++)
{
if(prices[i] - min > max) max = prices[i] - min;
if(prices[i] < min) min = prices[i];
}
return max;
}
};

小结

Best Time to Buy and Sell Stock - LeetCode的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

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

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

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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. C# LINQ 详解 From Where Select Group Into OrderBy Let Join

    目录 1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. ...

  2. zuul简单使用

    zuul路由的几个配置参数1.静态路由 zuul: routes: myroute1: path: /mypath/** url: http://localhost:8080 (注意这里url要htt ...

  3. LNMP V1.4一键快速部署Let's Encrypt免费SSL证书

    老左年前在"军哥LNMP V1.4测试版一键脚本安装以及功能上的升级体验"已经简单的体验到目前还没有正式版本的V1.4版本,理论上会在今年儿童节正式上线.从体验和实际的版本变动文档 ...

  4. vim-plug 插件安装与操作

    安装 vim-plug curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/juneg ...

  5. Python:线程之定位与销毁

    背景 开工前我就觉得有什么不太对劲,感觉要背锅.这可不,上班第三天就捅锅了. 我们有个了不起的后台程序,可以动态加载模块,并以线程方式运行,通过这种形式实现插件的功能.而模块更新时候,后台程序自身不会 ...

  6. ABPZero中的Name和SurName处理,以及EmailAddress解决方案(完美)。

    使用ABPzero的朋友们都知道,User表中有Name和Surname两个字段,这两个字段对于国内的用户来说相当的不友好. 以及我们的一些系统中是不会涉及到EmailAddress字段.也就是说不会 ...

  7. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  8. MRT与MRTS工具官宣退休,推荐使用HEG

    今天错误的删除搞丢了之前下载的MRT与MRTS工具,浏览Modis官网下载时发现找不到了,后来在其官网上发现了这则通知,原来早已停止更新的MRT这次彻底退修了.通知原文如下~~~ The downlo ...

  9. Docker容器学习梳理 - 日常操作总结

    使用Docker已有一段时间了,今天正好有空梳理下自己平时操作Docker时的一些命令和注意细节: Docker 命令帮助 $ sudo docker Commands: attach Attach ...

  10. OSGB数据压缩

    OSGB数据输出时压缩数据大小,采用如下设置 osgDB::writeNodeFile(*osgbNode, "xxx/xxxx.osgb", new osgDB::Options ...