-
Notifications
You must be signed in to change notification settings - Fork 0
add: Best Time to Buy and Sell Stock #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ```go | ||
| // For each day, | ||
| // We need to check if it's day that we can buy the stock with lower price | ||
| // And then check what if we sell it with the lowest price that we found in the past | ||
| // Time complexity: O(N) N: number of prices | ||
| // Space complexity: 1 | ||
| // Time spend: 04:32 | ||
| func maxProfit(prices []int) int { | ||
| buy := -prices[0]; | ||
| sell := buy + prices[0]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この部分、何か考えて初期化していますか。 |
||
|
|
||
| for _, p := range prices { | ||
| buy = max(buy, -p) | ||
| sell = max(sell, buy + p) | ||
| } | ||
|
|
||
| return sell | ||
| } | ||
|
|
||
| func max(a, b int) int { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go全然知らないのですが現在は組み込み関数のmaxがあるようです? |
||
| if a > b { | ||
| return a | ||
| } | ||
| return b | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| ```go | ||
| // Time spend: 01:10 | ||
| func maxProfit(prices []int) int { | ||
| buy := -prices[0] | ||
| sell := buy + prices[0] | ||
|
|
||
| for _, p := range prices { | ||
| buy = max(buy, -p) | ||
| sell = max(sell, buy + p) | ||
|
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. buy, sellがそれぞれ最小購入価格、最大売却価格の意味を持つことを変数名で表すとわかりやすいかと思いました。 |
||
| } | ||
|
|
||
| return sell | ||
| } | ||
|
|
||
| func max(a, b int) int { | ||
| if a < b { | ||
| return b | ||
| } | ||
| return a | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| ```go | ||
| // Time spend: 01:00 | ||
| package main | ||
| func maxProfit(prices []int) int { | ||
| buy := -prices[0] | ||
| sell := buy + -prices[0] | ||
|
|
||
| for _, p := range prices { | ||
| buy = max(buy, -p) | ||
| sell = max(sell, buy + p) | ||
| } | ||
|
|
||
| return sell | ||
| } | ||
|
|
||
| func max(a, b int) int { | ||
| if a > b { | ||
| return a | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ```go | ||
| // Time spend: 01:02 | ||
| package main | ||
| func maxProfit(prices []int) int { | ||
| buy := -prices[0] | ||
| sell := buy + -prices[0] | ||
|
|
||
| for _, p := range prices { | ||
| buy = max(buy, -p) | ||
| sell = max(sell, buy + p) | ||
| } | ||
|
|
||
| return sell | ||
| } | ||
|
|
||
| func max(a, b int) int { | ||
| if a > b { | ||
| return a | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ```go | ||
| // Time spend: 00:55 | ||
| package main | ||
| func maxProfit(prices []int) int { | ||
| buy := -prices[0] | ||
| sell := buy + -prices[0] | ||
|
|
||
| for _, p := range prices { | ||
| buy = max(buy, -p) | ||
| sell = max(sell, buy + p) | ||
| } | ||
|
|
||
| return sell | ||
| } | ||
|
|
||
| func max(a, b int) int { | ||
| if a > b { | ||
| return a | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、負の値を入れたのが、min を定義するのが億劫という理由だけのように見えていますが、なにか理由ございますでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
購入価格を負の数で表すのはなるほどとなりました!
個人的には正の数で表して、利益は引き算で計算するやり方が直感的でした。