LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载。
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的更多相关文章
- [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 ...
 - 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 ...
 - [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 ...
 - [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 ...
 - [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 ...
 - [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 ...
 - [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 ...
 - 【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 ...
 - [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 ...
 
随机推荐
- centos远程访问mssql数据库
			
http://blog.path8.net/archives/5921.html http://www.jaggerwang.net/2013/03/18/centos%E4%B8%8B%E5%AE% ...
 - 20155334 2016-2017-2 《Java程序设计》第七周学习总结
			
20155334 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章:Lambda 认识Lammbda语法 Lambda去可以重复,符合DRY原则,而且 ...
 - spring AOP的注解实例
			
上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...
 - Git冲突:commit your changes or stash them before you can merge. 解决办法
			
用git pull来更新代码的时候,遇到了下面的问题: 1 2 3 4 error: Your local changes to the following files would be overwr ...
 - 【运维技术】Nginx安装教程(yum安装,源码编译)
			
安装方式 yum直接更新源安装 源码直接编译之后安装 使用yum进行直接安装 Installing a Prebuilt CentOS/RHEL Package from an OS Reposito ...
 - shell指令操作memcached
			
shell指令操作memcached,可以用来直接测试memcached. 初始值为1000 #set test-value =1000printf "set test-value 0 0 ...
 - js输出大段html文档简便方法
			
原文链接:https://zhidao.baidu.com/question/586477237.html 把要输出的html全部写在某个id中,然后复制过去,你想放多少都行 <script t ...
 - ZLYD团队第5周项目总结
			
ZLYD团队第5周项目总结 项目进展 目前游戏人没有成功运行.初步判断是部分代码有误. 我们采用了两种运行方式,代码未出现明确错误.但问题可能是由于版本问题. 将Wall.java.Gold.java ...
 - nvm命令行操作命令
			
1,nvm nvm list 是查找本电脑上所有的node版本 - nvm list 查看已经安装的版本 - nvm list installed 查看已经安装的版本 - nvm list avail ...
 - nohup 不生成 nohup.out的方法
			
nohup java -jar /xxx/xxx/xxx.jar >/dev/>& & 关键在于最后的 >/dev/>& 部分,/dev/null是一个 ...