Leetcode 121 Best Time to Buy and Sell Stock

来源:互联网 发布:详解进化论知乎 编辑:程序博客网 时间:2024/06/06 01:23

Leetcode 121 Best Time to Buy and Sell Stock

#include <vector>#include <math.h>using namespace std;class Solution {public:    int maxProfit(vector<int>& prices)//brute force time limit exceeded    {    if(prices.empty())        return 0;    int maxprofit = 0;    int size = prices.size();    int buyPrice = prices[0];    for(int i = 1;i < size;i ++)    {        buyPrice = min(buyPrice,prices[i]);        maxprofit = max(maxprofit,prices[i] - buyPrice);    }    return maxprofit;    }};
原创粉丝点击