Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

buy and sell stock

class Solution {
    public int maxProfit(int[] prices) {
        int minimum=Integer.MAX_VALUE;
        int maxprofit=0;
        for(int i=0;i<prices.length;i++){
            if(prices[i]<minimum) minimum=prices[i];
            else if(prices[i]-minimum>maxprofit)    maxprofit=prices[i]-minimum;
        }
        return maxprofit;
        
    }
}
 
PREVIOUS NEXT
Tagged: #buy #sell #stock
ADD COMMENT
Topic
Name
3+7 =