#121.Best Time to Buy and Sell Stock [LeetCode Grind 75 in Java]

[Problem Link] https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int buy = 0;
        int maxP = 0;

        for(int i = 1 ; i < n ; i ++){
            //if current price is less than buying price -> update buying price
            if(prices[i] < prices[buy]) buy = i;
            else{
                int profit = prices[i] - prices[buy];
                maxP = Math.max(profit , maxP);
            }
        }
        return maxP;

    }
}