My JAVA Solution easy to understand


  • 0
    P
     public int maxProfit(int[] prices) {
            if(prices.length==0)   return 0;
            int mp=prices[0];
            int pro=0;
            for(int i=1;i<prices.length;i++){
            	if(prices[i]-mp>pro)	pro=prices[i]-mp;
            	if(prices[i]<mp)	mp=prices[i];  	
         }
            return pro;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.