Skip to content

122. Best Time to Buy and Sell Stock II#31

Open
n6o wants to merge 1 commit intomainfrom
best-time-to-buy-and-sell-stock-ii
Open

122. Best Time to Buy and Sell Stock II#31
n6o wants to merge 1 commit intomainfrom
best-time-to-buy-and-sell-stock-ii

Conversation

@n6o
Copy link
Copy Markdown
Owner

@n6o n6o commented Mar 24, 2026

今回の問題

Best Time to Buy and Sell Stock II - LeetCode

使用言語

Python

Comment on lines +46 to +47
for i in range(len(prices) - 1):
profit = prices[i + 1] - prices[i]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが「i 日目の利益 (profit) は、i + 1 日目での売値 - i 日目の買値」よりも、「i + 1日目の利益は〜」の方が、自然言語での説明に近いと思いました。

Suggested change
for i in range(len(prices) - 1):
profit = prices[i + 1] - prices[i]
for i in range(1, len(prices)):
profit = prices[i + 1] - prices[i]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rangeの範囲をそのようにするとprofit

profit = prices[i] - prices[i - 1]

にしないと上手くいかない気がします.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そうですね、訂正ありがとうございます 🙇

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

見込み利益、みたいなことを考えていたのでループの範囲に違和感を持ちませんでした。
コードでは表現できていないので、その辺まで考えて提示いただいた方がより読みやすいですね。

Comment on lines +46 to +47
for i in range(len(prices) - 1):
profit = prices[i + 1] - prices[i]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rangeの範囲をそのようにするとprofit

profit = prices[i] - prices[i - 1]

にしないと上手くいかない気がします.

Comment on lines +97 to +100
- `return max_return_without_stock` でも正しい
- ただ、パッとは理解できなかった
- max を使うことによるペナルティは特に大きくない気がするので、何も考えずに書いてしまう気がする
- 気を回せるようになるのがもちろん良い
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

最終日に株を持っていてもその後売れないから高かろうが安かろうがその時点で売った方が手元の現金は多い,という感覚で書いていました.今見ると私のコメントはトートロジーというか,説明になっていないですね...笑.

@5ky7
Copy link
Copy Markdown

5ky7 commented Mar 25, 2026

読みやすかったです.

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.

3 participants