-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathbest_time_to_buy_and_sell_stock.go
39 lines (34 loc) · 1.26 KB
/
best_time_to_buy_and_sell_stock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Best Time to buy and sell stock
/*
Explanation:
We start by initializing the minimum price to the maximum integer value and the maximum profit to 0.
We loop through the prices array, and for each price:
If the price is less than the current minimum price, we update the minimum price.
Otherwise, if the difference between the price and the minimum price is greater than the current maximum profit, we update the maximum profit.
Finally, we return the maximum profit
Sample Input [7, 1, 5, 3, 6, 4]
Output: 5 buy at 1 sell at 6
Time Complexity: O(n), where n is the length of the prices array.
Space Complexity: O(1), as we are only using two variables to keep track of the minimum price and maximum profit
*/
package main
import (
"fmt"
"math"
)
func maxProfit(prices []int) int {
minPrice := math.MaxInt32 // Initialize minimum price to maximum integer value
maxProfit := 0 // Initialize maximum profit to 0
for _, price := range prices {
if price < minPrice {
minPrice = price // Update minimum price
} else if price-minPrice > maxProfit {
maxProfit = price - minPrice // Update maximum profit
}
}
return maxProfit // Return maximum profit
}
func main() {
prices := []int{7, 1, 5, 3, 6, 4}
fmt.Println(maxProfit(prices)) // Output: 5
}