Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

buy and sell stocks

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
	    if not prices:
		    return 0

	    maxProfit = 0
	    minPurchase = prices[0]
	    for i in range(1, len(prices)):		
		    maxProfit = max(maxProfit, prices[i] - minPurchase)
		    minPurchase = min(minPurchase, prices[i])
	    return maxProfit    
 
PREVIOUS NEXT
Tagged: #buy #sell #stocks
ADD COMMENT
Topic
Name
5+6 =