[leetcode]Best Time to Buy and Sell Stock

weixin_30924079發表於2020-04-04

 

#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

轉載於:https://www.cnblogs.com/lihaozy/archive/2012/12/19/2825518.html

相關文章