jeudi 11 juillet 2019

How to fix runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h) in LeetCode?

This is the code and I can't understand where is it going out of bound for this error.

class Solution {
public:
int maxProfit(vector<int>& prices) {
    int min=prices[0],profit=0,best_profit=0;
    int n=prices.size();
    if (n<2) return 0;
    for(int i=1;i<n;i++){
        profit=prices[i]-min;
        best_profit = (profit > best_profit) ? profit : best_profit;

       if(min>prices[i])
            min=prices[i];
    }
    return best_profit;

}
};

Aucun commentaire:

Enregistrer un commentaire