Skip to content

Commit 70499d4

Browse files
committed
Best Time to Buy and Sell Stock II
1 parent c24ec52 commit 70499d4

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 122. Best Time to Buy and Sell Stock II
2+
// Topics: 'Array', 'Dynamic Programming' 'Greedy'
3+
// Level: 'Medium'
4+
5+
// You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
6+
7+
// On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can sell and buy the stock multiple times on the same day, ensuring you never hold more than one share of the stock.
8+
9+
// Find and return the maximum profit you can achieve.
10+
11+
// Example 1:
12+
13+
// Input: prices = [7,1,5,3,6,4]
14+
// Output: 7
15+
// Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
16+
// Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
17+
// Total profit is 4 + 3 = 7.
18+
19+
// Example 2:
20+
21+
// Input: prices = [1,2,3,4,5]
22+
// Output: 4
23+
// Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
24+
// Total profit is 4.
25+
26+
// Example 3:
27+
28+
// Input: prices = [7,6,4,3,1]
29+
// Output: 0
30+
// Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
31+
32+
// Constraints:
33+
34+
// 1 <= prices.length <= 3 * 104
35+
// 0 <= prices[i] <= 104
36+
37+
package besttimetobuyandsellstockii
38+
39+
func maxProfit(prices []int) int {
40+
sum := 0
41+
b := prices[0]
42+
for R := 1; R < len(prices); R++ {
43+
if prices[R]-b <= 0 {
44+
b = prices[R]
45+
} else {
46+
sum += (prices[R] - b)
47+
b = prices[R]
48+
}
49+
}
50+
return sum
51+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package besttimetobuyandsellstockii
2+
3+
import "testing"
4+
5+
func TestMaxProfit(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
prices []int
9+
want int
10+
}{
11+
{
12+
name: "example 1 - multiple transactions",
13+
prices: []int{7, 1, 5, 3, 6, 4},
14+
want: 7,
15+
},
16+
{
17+
name: "example 2 - continuous upward trend",
18+
prices: []int{1, 2, 3, 4, 5},
19+
want: 4,
20+
},
21+
{
22+
name: "example 3 - continuous downward trend",
23+
prices: []int{7, 6, 4, 3, 1},
24+
want: 0,
25+
},
26+
{
27+
name: "single day",
28+
prices: []int{5},
29+
want: 0,
30+
},
31+
{
32+
name: "two days - profit",
33+
prices: []int{1, 5},
34+
want: 4,
35+
},
36+
{
37+
name: "two days - loss",
38+
prices: []int{5, 1},
39+
want: 0,
40+
},
41+
{
42+
name: "two days - same price",
43+
prices: []int{5, 5},
44+
want: 0,
45+
},
46+
{
47+
name: "zigzag pattern",
48+
prices: []int{1, 7, 2, 8, 3, 9},
49+
want: 18, // (7-1) + (8-2) + (9-3) = 6 + 6 + 6
50+
},
51+
{
52+
name: "valley and peak",
53+
prices: []int{3, 2, 1, 4, 5, 6},
54+
want: 5, // buy at 1, sell at 6
55+
},
56+
{
57+
name: "multiple small gains",
58+
prices: []int{1, 2, 1, 2, 1, 2},
59+
want: 3, // three gains of 1 each
60+
},
61+
{
62+
name: "flat then up",
63+
prices: []int{5, 5, 5, 10},
64+
want: 5,
65+
},
66+
{
67+
name: "up then flat",
68+
prices: []int{1, 10, 10, 10},
69+
want: 9,
70+
},
71+
{
72+
name: "all zeros",
73+
prices: []int{0, 0, 0, 0},
74+
want: 0,
75+
},
76+
{
77+
name: "starting from zero",
78+
prices: []int{0, 1, 2, 3},
79+
want: 3,
80+
},
81+
{
82+
name: "large single jump",
83+
prices: []int{1, 10000},
84+
want: 9999,
85+
},
86+
{
87+
name: "many small ups and downs",
88+
prices: []int{5, 1, 5, 1, 5, 1, 5},
89+
want: 12, // three cycles of 4 profit each
90+
},
91+
{
92+
name: "peak in middle",
93+
prices: []int{1, 5, 3, 7, 2},
94+
want: 8, // (5-1) + (7-3) = 4 + 4
95+
},
96+
{
97+
name: "alternating by one",
98+
prices: []int{1, 2, 1, 2, 1, 2, 1},
99+
want: 3,
100+
},
101+
{
102+
name: "max constraint values",
103+
prices: []int{10000, 0, 10000, 0, 10000},
104+
want: 20000,
105+
},
106+
{
107+
name: "gradual increase then decrease",
108+
prices: []int{1, 2, 3, 4, 3, 2, 1},
109+
want: 3, // sell at peak (day 4)
110+
},
111+
}
112+
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
got := maxProfit(tt.prices)
116+
if got != tt.want {
117+
t.Errorf("maxProfit(%v) = %d, want %d", tt.prices, got, tt.want)
118+
}
119+
})
120+
}
121+
}
122+
123+
// Test to verify the greedy approach
124+
func TestMaxProfitGreedyLogic(t *testing.T) {
125+
// This test verifies that capturing every upward movement
126+
// is equivalent to the optimal strategy
127+
prices := []int{1, 3, 2, 4, 1, 5}
128+
// Upward movements: (3-1)=2, (4-2)=2, (5-1)=4
129+
// Total: 2 + 2 + 4 = 8
130+
want := 8
131+
got := maxProfit(prices)
132+
133+
if got != want {
134+
t.Errorf("maxProfit(%v) = %d, want %d (sum of all positive diffs)", prices, got, want)
135+
}
136+
}

0 commit comments

Comments
 (0)