Best Time to Buy and Sell Stock
来源:互联网 发布:三端口光纤环形器 编辑:程序博客网 时间:2024/05/22 04:57
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.
思路保存当前最小值和当前最大收益,扫描更新。
class Solution {public: int maxProfit(vector<int> &prices) { if (prices.size() == 0) { return 0; } int minValue = prices[0]; int maxProfit = 0; for (int i = 0; i < prices.size(); i++) { if (prices[i] < minValue) { minValue = prices[i]; } if (prices[i] - minValue > maxProfit) { maxProfit = prices[i] - minValue; } } return maxProfit; }};
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- ThoughtWorks待遇
- Integer.valueOf(Integer intObj)运行过程及引发的其它问题
- 6.4 Git 工具 - 重写历史
- (mini2440)建立交叉编译环境+配置linux内核
- hdu 5015 233 Matrix 矩阵快速幂
- Best Time to Buy and Sell Stock
- project facet java version 1.8
- Node.js v0.10.31API手册-简介
- HDU 5023 A Corrupt Mayor's Performance Art
- 奇偶剪枝
- Linux 添加开机启动项的两种方法
- 推荐13款JavaScript图形和图表绘制工具
- 数据时代的企业管理——读《量化》的随手札记
- 6.5 Git 工具 - 使用 Git 调试