This is a solution I wrote on leetcode to find the maximum profit in an array. When I submit it will give me a runtime error. After spending some time debugging, I found my code gets stuck in the if statement pointed out below. I can't seem to find any incorrect syntax use. So where did I do wrong?
int maxProfit(vector<int>& prices) {
int profit=0;
for(int i=0; i<prices.size()-1; i++){
if(prices[i+1] > prices[i]){ // <--stuck here;
profit += (prices[i+1] - prices[i]);
}
}
return profit;
}
Aucun commentaire:
Enregistrer un commentaire