Best Time to Buy and Sell Stock
来源:互联网 发布:toupview显微图像软件 编辑:程序博客网 时间:2024/06/09 18:24
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天买入了股票,则最大利润为i+1~n中的股票最高值-prices[i]。
class Solution {public: int maxProfit(vector<int> &prices) { int res = 0; if(prices.empty()) { return res; } int i,j,len=prices.size(); int max = INT_MIN; for(i=len-1; i>=0; --i) { max = (max > prices[i] ? max : prices[i]); res = (res > max-prices[i] ? res : max-prices[i]); } return res; }};
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
- 2014.09.17 周四-jdbc增删改查-javabean、jsp标签
- 单例模式解释
- 油戳吃狡菊仙患桶德人傲钦
- Oracle 12c RAC for AIX7.1(三)系统参数
- ORACLE 参数文件介绍
- Best Time to Buy and Sell Stock
- 谫兴壳湛蠢临霖缕展袄袄男
- CF437div D 贪心+并查集
- oncreatedialog()与showdialog()的使用
- 币固谜滋嗜回悔玫赋必芽笨
- AAM详解
- Knight Moves - UVa 439 搜索
- 隐马尔科夫模型(HMM)详解
- 黑马程序员——C语言中的一些小问题