'some error in,, Buy and sell stocks with cooldown

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

This code is giving run time error due to second condition which i have added in if statement, can anyone explain why?

class Solution {
 public:
  int maxProfit(vector<int>& prices) {
    int n = prices.size();
    if (n == 0)
      return 0;

    int buy, sell = 0;
    int profit = 0;
    int i = 1;
    while (sell != n - 1) {
      if (prices[i] > prices[i - 1] && prices[i + 1] > prices[i]) {
        sell++;
        profit += prices[i] - prices[i - 1];
        buy++;

      } else if (prices[i + 1] > prices[i]) {
        // buy=sell;

        sell++;
        profit += prices[i + 1] - prices[i];
      }

      i++;
    }
    return profit;
  }
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source