[leetcode]Best Time to Buy and Sell Stock

 

#include <iostream>
#include <vector>
#include <stack>
using namespace std;


//简单的DP算法
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int size = prices.size();
        if (!size)
            return 0;

        vector<int> min(size);
        min[0] = prices[0];
        for (int i = 1; i < size; i++){
            min[i] = std::min(min[i-1], prices[i]);
        }

        int max = 0;
        for (int i = 1; i < size; i++){
            max = std::max(max, prices[i] - min[i]);
        }

        return max;
    }
};


int main()
{
    
    return 0;
}

 

 

 

 

 

EOF

posted on 2012-12-19 21:10  kkmm  阅读(169)  评论(0编辑  收藏  举报