Best Time to Buy and Sell Stock using Go

 In today practice, i try to solve this question from leetcode, In first few attempts, my solution not work, mostly its throw TLE error, after some help from internet i able to solve this question, (t took help because i don't want to waste too much time on a single question.).

Question:


Solution:

func maxProfit(prices []int) int {
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
minProfit := MaxInt
maxProfit := 0

for i := 0; i < len(prices); i++ {
if prices[i] < minProfit {
minProfit = prices[i]
continue
}
if prices[i]-minProfit > maxProfit {
maxProfit = prices[i] - minProfit
}
}
return maxProfit
}


Post a Comment

0 Comments