Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Feb 2, 2025

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.

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:

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空の場合の制御は必要になりますがループ内のコードは短くなると思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ループの方をprice[1]からスタートする感じでしょうか。
確かにその方がシンプルですね

return profit
```

- テストケースを考える(実は今まであんまりやってなかった😅)

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はよく似て関数だが、ここを無理やり共通化しようとするのは流石にややこしい?
Copy link

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, ,,,,,,]
- くらいかな。うーん。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分は以前、理事の川中さんが「テストケースはシステマティックに生成すると良い」とおっしゃっていたのを参考にテストケースを考えています

Copy link
Owner Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

消し忘れでしょうか。

Copy link
Owner Author

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
Copy link

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) でもよさそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに。ありがとうございます。
簡潔な一方、やや読み手に推理が必要な気もしますが、これぐらいなら許容範囲内ですかね

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants