-
Notifications
You must be signed in to change notification settings - Fork 0
121. Best Time to Buy and Sell Stock #32
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,24 @@ | ||
| # step1 何も見ずに解く | ||
|
|
||
| ある日に売る時に得られる最大の利益は、「ある日での売り値 - ある日以前での最安値」で求めることができる。 | ||
| ある日以前の最安値と、最大の利益を変数で更新していけば時間計算量O(N)となり、Nの最大値は10^5なので1秒以内に間に合う。 | ||
| 空間計算量はO(1) | ||
|
|
||
| ```ruby | ||
| # @param {Integer[]} prices | ||
| # @return {Integer} | ||
| def max_profit(prices) | ||
| prices_size = prices.size | ||
| return 0 if prices_size <= 1 | ||
|
|
||
| min_price = prices.first | ||
| max_profit = 0 | ||
| (1...(prices_size)).each do |i| | ||
| price = prices[i] | ||
| max_profit = [max_profit, price - min_price].max | ||
| min_price = [min_price, price].min | ||
| end | ||
| max_profit | ||
| end | ||
| ``` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # step2 他の方の解答を見る | ||
|
|
||
| https://github.com/hayashi-ay/leetcode/pull/52/files#diff-0474f0ee7711182f0e97bb4047531dc4c65356748eafab139512400ac88c5c0bR67-R79 | ||
|
|
||
| 後ろから見て売る時の最高値を更新していき、最安値の時に買うパターン。 | ||
| 個人的には買う->売るという作業の流れを逆にしたくないので好みではない。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| step1と同じになった。 | ||
| ```ruby | ||
| # @param {Integer[]} prices | ||
| # @return {Integer} | ||
| def max_profit(prices) | ||
| prices_size = prices.size | ||
| return 0 if prices_size <= 1 | ||
|
|
||
| min_price = prices.first | ||
| max_profit = 0 | ||
| (1...(prices_size)).each do |i| | ||
| price = prices[i] | ||
| max_profit = [max_profit, price - min_price].max | ||
| min_price = [min_price, price].min | ||
| end | ||
| max_profit | ||
| end | ||
| ``` | ||
|
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. price[i] が min_price より小さい場合は max_profit の更新はないため、min_price を更新して次のループに進むのもよさそうかなと思いました。 def max_profit(prices)
prices_size = prices.size
return 0 if prices_size <= 1
min_price = prices.first
max_profit = 0
(1...prices_size).each do |i|
if prices[i] < min_price
min_price = prices[i]
next
end
max_profit = [max_profit, prices[i] - min_price].max
end
max_profit
end
Owner
Author
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. コメントありがとうございます! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
試せていないので間違っていたら申し訳ないのですが、この行はなくても良い気がしました。
.each doの部分が回らずにそのまま0が出力されるのではないでしょうか。
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.
コメントありがとうございます!
おっしゃる通りなくても良いのですがサイズが1以下の時の処理をわかりやすくするために残していました。