版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/xiezhihua120/article/details/32939749

Say you have an array for which the ith 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.

分析:维护一个局部最小值,迭代扫描数组,当在 i 位置卖出时值须要知道过去的最低价minPrices就可以,在i位置得到最大收益。多次迭代求出在n-1位置得最大收益。

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

此处须要注意測试用例,当没有元素或者仅仅有一个元素时,最大获利为0

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

  1. [LeetCode OJ] Best Time to Buy and Sell Stock I

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

  2. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  3. [LeetCode] 121. 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 ...

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

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

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

    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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 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] 309. 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 IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

  9. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

随机推荐

  1. echarts 不同区域背景色不同 废了我一天的时间

    var result = echarts.init(document.getElementById('result')); var option1 = { title: { text: '设备阶段故障 ...

  2. 20145204《Java程序设计》第3周学习总结

    20145204<Java程序设计>第3周学习总结 教材学习内容总结 对象和类. Java有基本类型和类类型这两个类型系统.本章主要介绍类类型.定义类时用关键词class,利用类建立对象实 ...

  3. 20145221 《Java程序设计》实验报告三:敏捷开发与XP实践

    20145221 <Java程序设计>实验报告三:敏捷开发与XP实践 实验要求 以结对编程的方式编写一个软件,Blog中要给出结对同学的Blog网址 记录TDD和重构的过程,测试代码不要少 ...

  4. stm32 Flash读写独立函数[库函数]

    一. stm32的FLASH分为 1.主存储块:用于保存具体的程序代码和用户数据,主存储块是以页为单位划分的, 一页大小为1KB.范围为从地址0x08000000开始的128KB内. 2.信息块   ...

  5. centos7下SVN服务器如何搭建

    Centos7 搭建svn服务器 linux(centos)下SVN服务器如何搭建?说到SVN服务器,想必大家都知道,可以是在LINUX下如何搭建SVN服务器呢?那么今天给大家分享一下linux(ce ...

  6. 山东省第四届ACM程序设计竞赛部分题解

    A : Rescue The Princess 题意: 给你平面上的两个点A,B,求点C使得A,B,C逆时针成等边三角形. 思路: http://www.cnblogs.com/E-star/arch ...

  7. shell 判断文件是否是可执行文件

    测试变量指定的文件是否存在且是可执行文件.如果存在且是可执行文件,则执行该文件,否则通过chmod命令赋予该文件可执行权限. //test.sh #!/bin/bash echo "ente ...

  8. [原][osgearth]API加载earth文件的解析

    参考:http://blog.csdn.net/cccstudyer/article/details/17691893 通过\src\osgEarthDrivers\earth\ReaderWrite ...

  9. 2-15-MySQL进阶

    select select 字段列表 from 数据表 [[as] 别名] [where 条件] 别名: 数据表 [[as] 别名] select AA.money,BB.name from prod ...

  10. 微信小程序纯css制作圆形进度条所遇到的问题

    wrapBox:最外层盒子,背景色为进度条的颜色 leftBox/rightBox:半宽等长 左/右浮动的盒子,背景色为灰色 roundMask:居中的盒子 用来遮盖leftBox和rightBox ...