首页 > 代码库 > LeedCode --- Best Time to Buy and Sell Stock
LeedCode --- Best Time to Buy and Sell Stock
题目链接
题意: find the maximum positive difference between the price on the ith day and the jth day
附上代码:
1 class Solution {
2 public:
3 int maxProfit(vector<int> &prices) {
4 if (prices.size() == 0)
5 return 0;
6 // "minimum" holds the minimum price before the ith day.
7 // "max_diff" holds the maximum difference between prices[i] and prices[j]
8 // where 0 <= i < j < prices.size()
9 int minimum = prices[0], max_diff = 0;
10 for (unsigned int i = 1; i < prices.size(); i++) {
11 if (prices[i] - minimum > max_diff) {
12 max_diff = prices[i] - minimum;
13 }
14 if (prices[i] < minimum) {
15 minimum = prices[i];
16 }
17 }
18 return max_diff;
19 }
20 };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。