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

121. Best Time to Buy and Sell Stock #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from best-time-to-buy-and-sell-stock
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ continually updating 😃.
* [75. Sort Colors](./src/0075_sort_colors/sort_colors.go)   *`sort;`*  *`array`*
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
* [88. Merge Sorted Array](./src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
Expand Down
59 changes: 59 additions & 0 deletions src/0121_best_time_to_buy_and_sell_stock/maxprofit.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
121. Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction
(i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.
*/
// time: 2018年12月28日

package maxprofit

// dynamic programming
// for every price, find the max profit, then record the current minimum price.
// time complexity: O(n)
// space complexity: O(1)
func maxProfit(prices []int) int {
n := len(prices)
if 0 == n || 1 == n {
return 0
}

var (
res int
minPrice = prices[0]
)

for _, price := range prices {
if price - minPrice > res {
res = price - minPrice
}
if price < minPrice {
minPrice = price
}
}
return res
}

// brute force
// time complexity: O(n^2)
// space complexity: O(1)
func maxProfit1(prices []int) int {
n := len(prices)
if 0 == n || 1 == n {
return 0
}
res := 0
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if prices[i]-prices[j] > res {
res = prices[i] - prices[j]
}
}
}
return res
}
39 changes: 39 additions & 0 deletions src/0121_best_time_to_buy_and_sell_stock/maxprofit_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package maxprofit

import "testing"

func TestMaxProfit(t *testing.T) {
testCases := [][]int{
{7, 1, 5, 3, 6, 4},
{5, 3, 2, 2, 5, 7, 9, 4},
{},
{3},
{5, 3, 2, 2, 5, 7, 9, 4,5, 3, 2, 2, 5, 7, 9, 4},
{2,4,1,11,7},
}

expected := []int{5, 7, 0, 0,7,10}

for index, data := range testCases {
if res := maxProfit(data); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}

func TestMaxProfit1(t *testing.T) {
testCases := [][]int{
{7, 1, 5, 3, 6, 4},
{5, 3, 2, 2, 5, 7, 9, 4},
{},
{3},
}

expected := []int{5, 7, 0, 0}

for index, data := range testCases {
if res := maxProfit1(data); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
|0111|[Minimum Depth of Binary Tree](./0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)|Easy|*`binary tree`*|
|0112|[Path Sum](./0112_path_sum/path_sum.go)|Easy|*`binary tree`*|
|0120|[Triangle](./0120_triangle/triangle.go)|Medium|*`dynamic programming;`* *` dfs`*|
|0121|[121. Best Time to Buy and Sell Stock](0121_best_time_to_buy_and_sell_stock/maxprofit.go)|Easy||
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
Expand Down

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