-
Notifications
You must be signed in to change notification settings - Fork 0
122. Best Time To Sell and Buy Stock Ⅱ #44
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?
Conversation
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day. Find and return the maximum profit you can achieve.
| is_having = False | ||
| prev_price = None | ||
| for price in prices: | ||
| if prev_price is None: |
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.
if prev_price is None:を使うのはループの1回目だけだと思いますので、prev_priceをprice[0]で初期化するのもアリかと思いました。prices空の場合の制御は必要になりますがループ内のコードは短くなると思います。
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.
ループの方をprice[1]からスタートする感じでしょうか。
確かにその方がシンプルですね
| return profit | ||
| ``` | ||
|
|
||
| - テストケースを考える(実は今まであんまりやってなかった😅) |
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.
同じくです。自分も気をつけようと思います。
|
|
||
| - 関数を定める方法、DPでやる方法などがある | ||
| - 関数を定める方法で書いてみる | ||
| - find_next_peakとfind_next_bottomはよく似て関数だが、ここを無理やり共通化しようとするのは流石にややこしい? |
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.
C++ には adjacent_find というのがありますね。
共通化しなくてもいいように思いますが。
| - [6, 5, 4, 5, 6, 5, 4, 5, 6,,,,,] | ||
| - [1, 2, 3, 4, 5, 6,,,,] | ||
| - [100, 99, 98, 97, ,,,,,,] | ||
| - くらいかな。うーん。 |
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.
自分は以前、理事の川中さんが「テストケースはシステマティックに生成すると良い」とおっしゃっていたのを参考にテストケースを考えています
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.
ありがとうございます。
そうおっしゃってましたよね〜、あとは全ての行を通るケース、空とかですかねえ
| - peakとbottomを交互にループするのは、最初にbottomをやった方が綺麗そう?気づかなかった。 | ||
|
|
||
| ```python | ||
| python |
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.
消し忘れでしょうか。
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.
そうですね、ありがとうございます(よくあるのでlinterとか導入したほうがいい気もしてきました)
| for i in range(len(prices) - 1): | ||
| diff = prices[i + 1] - prices[i] | ||
| if diff > 0: | ||
| profit += diff |
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.
ifを消して
profit += max(0, diff) でもよさそうです。
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.
確かに。ありがとうございます。
簡潔な一方、やや読み手に推理が必要な気もしますが、これぐらいなら許容範囲内ですかね
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.