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 a231349

Browse files
authored
121 best time to buy and sell stock solved. (#21)
1 parent 6f1e276 commit a231349

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ continually updating 😃.
1717
* [75. Sort Colors](./src/0075_sort_colors/sort_colors.go)   *`sort;`*  *`array`*
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`*
20+
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
2021
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
2122
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
2223
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
121. Best Time to Buy and Sell Stock
3+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
If you were only permitted to complete at most one transaction
8+
(i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
9+
10+
Note that you cannot sell a stock before you buy one.
11+
*/
12+
// time: 2018年12月28日
13+
14+
package maxprofit
15+
16+
// dynamic programming
17+
// for every price, find the max profit, then record the current minimum price.
18+
// time complexity: O(n)
19+
// space complexity: O(1)
20+
func maxProfit(prices []int) int {
21+
n := len(prices)
22+
if 0 == n || 1 == n {
23+
return 0
24+
}
25+
26+
var (
27+
res int
28+
minPrice = prices[0]
29+
)
30+
31+
for _, price := range prices {
32+
if price - minPrice > res {
33+
res = price - minPrice
34+
}
35+
if price < minPrice {
36+
minPrice = price
37+
}
38+
}
39+
return res
40+
}
41+
42+
// brute force
43+
// time complexity: O(n^2)
44+
// space complexity: O(1)
45+
func maxProfit1(prices []int) int {
46+
n := len(prices)
47+
if 0 == n || 1 == n {
48+
return 0
49+
}
50+
res := 0
51+
for i := 1; i < n; i++ {
52+
for j := 0; j < i; j++ {
53+
if prices[i]-prices[j] > res {
54+
res = prices[i] - prices[j]
55+
}
56+
}
57+
}
58+
return res
59+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
{5, 3, 2, 2, 5, 7, 9, 4},
9+
{},
10+
{3},
11+
{5, 3, 2, 2, 5, 7, 9, 4,5, 3, 2, 2, 5, 7, 9, 4},
12+
{2,4,1,11,7},
13+
}
14+
15+
expected := []int{5, 7, 0, 0,7,10}
16+
17+
for index, data := range testCases {
18+
if res := maxProfit(data); res != expected[index] {
19+
t.Errorf("expected %d, got %d", expected[index], res)
20+
}
21+
}
22+
}
23+
24+
func TestMaxProfit1(t *testing.T) {
25+
testCases := [][]int{
26+
{7, 1, 5, 3, 6, 4},
27+
{5, 3, 2, 2, 5, 7, 9, 4},
28+
{},
29+
{3},
30+
}
31+
32+
expected := []int{5, 7, 0, 0}
33+
34+
for index, data := range testCases {
35+
if res := maxProfit1(data); res != expected[index] {
36+
t.Errorf("expected %d, got %d", expected[index], res)
37+
}
38+
}
39+
}

‎src/README.md

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

0 commit comments

Comments
(0)

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