Best Time to Buy and Sell Stock always keep timeout


  • 0
    A

    #include<iostream>
    #include<algorithm>
    class Solution {
    public:
    int maxProfit(vector<int> &prices) {
    if(prices.size() < 2) return 0;
    int minprice = prices[0];
    int ans = 0;
    for (int i = 0; i < prices.size(); i++){
    minprice = min(minprice, prices[i]);
    ans = max(ans, prices[i] - min);
    }
    return ans;
    }
    };
    I have write my code like this, why does it always keep timeout?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.