LeetCode 121. Best Time to Buy and Sell Stock

 class Solution {

    public int maxProfit(int[] ar) {

        int b=ar[0];

        int s=0;

        int max=0;

        for(int x=1;x<ar.length;x++)

        {

            if(b>ar[x])

            {

                b=ar[x];

            }

            if(ar[x]-b>max)

            {

                max=ar[x]-b;

                    s=ar[x];

            }

        }

        return max;

    }

}

Comments

Popular Posts