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 ...
随机推荐
- react headtop title 截取
render() { const nav = this.props.nav const text = nav && nav.length > 5 ? this.strHandle ...
- Python笔记 #09# Basic plots with matplotlib
源:DataCamp 气泡的大小表示人口的多少,横坐标表示人均GDP(美元),纵坐标表示预期寿命.-- 作者:Hans Rosling Python 中有许许多多用于可视化的包,而 matplotli ...
- P2831 愤怒的小鸟(状压dp)
P2831 愤怒的小鸟 我们先预处理出每个猪两两之间(设为$u,v$)和原点三点确定的抛物线(当两只猪横坐标相等时显然无解) 处理出$u,v$确定的抛物线一共可以经过多少点,记为$lines[u][v ...
- Linux内核分析 02
二,操作系统是如何工作的 1.函数调用堆栈 三大法宝:存储程序计算机 函数调用堆栈 中断机制 堆栈:是C语言程序运行时必须的一个记录调用路径和参数的空间.是计算机内部现成的东西,我们直接使用. 包括函 ...
- 20145221 《Java程序设计》第四周学习总结
20145221 <Java程序设计>第四周学习总结 教材学习内容总结 第六章部分 - 继承与多态 何谓继承 继承 继承是Java程序设计语言面向对象的又一重要体现,允许子类继承父类,避免 ...
- 20145319 《网络渗透》MS12_020安全漏洞
20145319 <网络渗透>MS12_020安全漏洞 一 实验内容 初步掌握平台matesploit辅助模块aux的使用 辅助模块包括扫描等众多辅助功能 本次展示DOS攻击的实现 有了初 ...
- 20145325张梓靖 《Java程序设计》第7周学习总结
20145325张梓靖 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治时间,简称GMT时间,由观察太阳而得来:世界时,UT:国际原子时,TAI:世界协调时间,U ...
- vue中实现中,自动补全功能
知识点:利用vue的基本语法实现,自动补全功能 参考博客:https://www.jb51.net/article/136282.htm 效果:在文本框中,输入相关名称,调用后台接口,将数据填充到下拉 ...
- LA 3938 动态最大连续和(线段树)
https://vjudge.net/problem/UVALive-3938 题意:给出一个长度为n的整数序列D,你的任务是对m个询问作出回答.对于询问(a,b),需要找到两个下标x和y,使得a≤x ...
- UI 交互
动效设计 亮色优缺点 排版 原型图交互说明