码迷,mamicode.com
首页 > 其他好文 > 详细

LeedCode --- Best Time to Buy and Sell Stock

时间:2014-05-28 03:49:45      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

题目链接

题意: find the maximum positive difference between the price on the ith day and the jth day 

附上代码:

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

 

LeedCode --- Best Time to Buy and Sell Stock,布布扣,bubuko.com

LeedCode --- Best Time to Buy and Sell Stock

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/Stomach-ache/p/3753522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!