Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8d102b

Browse files
authored
122. Best Time to Buy and Sell Stock II (#22)
* 122 best time to buy and sell stock 2 solved. * add more ut case for 0122 best time to buy and sell stock 2.
1 parent 8385819 commit f8d102b

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ continually updating 😃.
1818
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
1919
* [88. Merge Sorted Array](./src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
2020
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
21+
* [122. Best Time to Buy and Sell Stock II](src/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)   *`greedy;`*  *`array`*
2122
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
2223
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
2324
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*

‎src/0121_best_time_to_buy_and_sell_stock/maxprofit.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func maxProfit(prices []int) int {
2424
}
2525

2626
var (
27-
res int
27+
res int
2828
minPrice = prices[0]
2929
)
3030

3131
for _, price := range prices {
32-
if price-minPrice > res {
32+
if price-minPrice > res {
3333
res = price - minPrice
3434
}
3535
if price < minPrice {

‎src/0121_best_time_to_buy_and_sell_stock/maxprofit_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ func TestMaxProfit(t *testing.T) {
88
{5, 3, 2, 2, 5, 7, 9, 4},
99
{},
1010
{3},
11-
{5, 3, 2, 2, 5, 7, 9, 4,5, 3, 2, 2, 5, 7, 9, 4},
12-
{2,4,1,11,7},
11+
{5, 3, 2, 2, 5, 7, 9, 4,5, 3, 2, 2, 5, 7, 9, 4},
12+
{2,4, 1, 11,7},
1313
}
1414

15-
expected := []int{5, 7, 0, 0,7,10}
15+
expected := []int{5, 7, 0, 0,7, 10}
1616

1717
for index, data := range testCases {
1818
if res := maxProfit(data); res != expected[index] {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
122. Best Time to Buy and Sell Stock II
3+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
Design an algorithm to find the maximum profit.
8+
You may complete as many transactions as you like
9+
(i.e., buy one and sell one share of the stock multiple times).
10+
11+
Note: You may not engage in multiple transactions at the same time
12+
(i.e., you must sell the stock before you buy again).
13+
*/
14+
// time: 2018年12月28日
15+
16+
package maxprofit
17+
18+
// greedy
19+
// time complexity: O(n)
20+
// space complexity: O(1)
21+
func maxProfit(prices []int) int {
22+
n := len(prices)
23+
24+
if 0 == n || 1 == n {
25+
return 0
26+
}
27+
28+
var (
29+
res int
30+
minPrice = prices[0]
31+
)
32+
for i := 1; i < n; i++ {
33+
if prices[i] < prices[i-1] {
34+
res += prices[i-1] - minPrice
35+
minPrice = prices[i]
36+
}
37+
if i == n-1 {
38+
res += prices[i] - minPrice
39+
}
40+
}
41+
return res
42+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package maxprofit
2+
3+
import "testing"
4+
5+
func TestMaxProfit(t *testing.T) {
6+
testCases := [][]int{
7+
{7, 1, 5, 3, 6, 4},
8+
{1, 2, 3, 4, 5},
9+
{7, 6, 4, 3, 1},
10+
{}, {1},
11+
}
12+
expected := []int{7, 4, 0, 0, 0}
13+
14+
for index, data := range testCases {
15+
if res := maxProfit(data); res != expected[index] {
16+
t.Errorf("expected %d, got %d", expected[index], res)
17+
}
18+
}
19+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
|0112|[Path Sum](./0112_path_sum/path_sum.go)|Easy|*`binary tree`*|
3434
|0120|[Triangle](./0120_triangle/triangle.go)|Medium|*`dynamic programming;`* *` dfs`*|
3535
|0121|[121. Best Time to Buy and Sell Stock](0121_best_time_to_buy_and_sell_stock/maxprofit.go)|Easy||
36+
|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|
3637
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
3738
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
3839
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /