This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple_stocks_test.go
74 lines (61 loc) · 1.87 KB
/
apple_stocks_test.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Problem:
- Given a list of stock prices (integer) in chronological order, return the
max profit from buying at earlier time and selling at later time.
Example:
- Input: []int{10, 7, 5, 8, 11, 9}
Output: 6, because one can buy at 5 and sell at 11
Approach:
- Use a greedy approach to keep track of the minimum price and the maximum
profit for each value while iterating through the list.
Solution:
- Initialize a minimum price to be the first price in the list and a maximum
profit to be the first possible profit that we could trade.
- Iterate through the list and keep track of:
- the current price
- the potential profit: current price - minimum price
- the maximum profit: the larger profit between the current maximum and the potential one
- the minimum price: the smaller price between the current minimum and the current price.
- Return the maximum profit we found in the end.
Cost:
- O(n) time, O(1) space.
*/
package interviewcake
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestGetMaxProfit(t *testing.T) {
tests := []struct {
in []int
expected int
}{
{[]int{}, 0},
{[]int{10}, 0},
{[]int{10, 10}, 0},
{[]int{10, 7, 5, 8, 11, 9}, 6},
{[]int{10, 2, 5, 4, 7, 1}, 5},
{[]int{10, 7, 2, 1}, -1},
}
for _, tt := range tests {
result := getMaxProfit(tt.in)
common.Equal(t, tt.expected, result)
}
}
func getMaxProfit(stocks []int) int {
// return 0 if the input is invalid
if len(stocks) <= 2 {
return 0
}
// initialize minPrice to be the first price in the list and maxProfit to
// be the first possible profit that we could trade.
minPrice := stocks[0]
maxProfit := stocks[1] - stocks[0]
for i := 1; i < len(stocks); i++ {
currentPrice := stocks[i]
potentialProfit := currentPrice - minPrice
maxProfit = common.Max(potentialProfit, maxProfit)
minPrice = common.Min(minPrice, currentPrice)
}
return maxProfit
}