题目链接

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. item 11: 比起private undefined function优先使用deleted function

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果你为其他开发者提供代码,并且你想阻止他们调用一个特定的函数,你 ...

  2. 【IDEA】Intellij IDEA创建的Web项目配置Tomcat并启动Maven项目

    转载请注明出处:http://blog.csdn.net/qq_26525215本文源自[大学之旅_谙忆的博客] 本篇博客讲解IDEA如何配置Tomcat. 大部分是直接上图哦. 点击如图所示的地方, ...

  3. Ionic 3 延迟加载(Lazy Load)实战(一)

    本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...

  4. spring-session-data-redis包冲突

    包冲突 spring 的包很容易冲突, 因为写软件的人在兼容性上处理的不够,一般不检测重复加载. spring-session-data-redis 引用后, 一定要把 spring-session ...

  5. Python进阶量化交易专栏场外篇7- 装饰器计算代码时间

    欢迎大家订阅<教你用 Python 进阶量化交易>专栏!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外已陆续推出一些手记来辅助同学们学习本专栏内容,目前已推出如下扩展篇: 在第一篇 ...

  6. Vuex实现原理解析

    我们在使用Vue.js开发复杂的应用时,经常会遇到多个组件共享同一个状态,亦或是多个组件会去更新同一个状态,在应用代码量较少的时候,我们可以组件间通信去维护修改数据,或者是通过事件总线来进行数据的传递 ...

  7. D. Little C Loves 3 II

    传送门 [http://codeforces.com/contest/1047/problem/D] 题意 给你n*m得棋盘,让你找两点之间距离为3的点的个数,不能重复使用,距离定义,两坐标差绝对值之 ...

  8. 11.14 Daily Scrum

    实现推荐菜谱时遇到问题,这个功能要和数据库和服务器有关,所以暂时放在一边,可能在beta版本实现. 部分成员已经完成基本任务,进行完善.   Today's Task Tomorrow's Task ...

  9. Linux内核分析作业第四周

    系统调用的三个层次 一.用户态.内核态和中断 用户通过库函数与系统调用联系起来. 1.内核态 在高的执行级别下,代码可以执行特权指令,访问任意的物理地址,这时的CPU就对应内核态 2.用户态: 在低级 ...

  10. Linux内核第八节 20135332武西垚

    第一种分类: I/O-bound:频繁进行I/O,并且需要花费很多时间等待I/O完成 CPU-bound:计算密集,需要大量的CPU时间进行运算 第二种分类: 批处理进程:不必与用户交互,常在后台进行 ...