Skip to content

Create 122. Best Time to Buy and Sell Stock II.md#38

Open
fuga-98 wants to merge 1 commit into
mainfrom
122.-Best-Time-to-Buy-and-Sell-Stock-II
Open

Create 122. Best Time to Buy and Sell Stock II.md#38
fuga-98 wants to merge 1 commit into
mainfrom
122.-Best-Time-to-Buy-and-Sell-Stock-II

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented Apr 22, 2025

Comment on lines +145 to +146
if i == num_prices - 1:
break
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この構造は while-else 使うときれいに書けますかね?

if i == num_prices - 1:
break
profit -= 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.

こういうコメントのおかげもあって、一つの(外側の)ループで1連の株売買をしていることがすぐ読み取れますし、手でやるならこうなると思います。
一方、実務で書くなら前日からの値上がり分を累積していく方法の方がシンプルに書けるので、私はそちらを選択すると思いました。

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.

レビューありがとうございます!
実務なら私も累積和を選択すると思います。

class Solution:
def maxProfit(self, prices: List[int]) -> int:
assert isinstance(prices, list)
if len(prices) <= 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.

このブロックはなくても動きますね。

result = 0
holding = prices[0]
for i in range(1, len(prices) - 1):
print(holding)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

print(f'i: {i}, holding: {holding}')
みたいにログを出すとデバッグに有用かもしれません。多くの情報をログに出すくらいならデバッガーを使った方がいいかもしれませんが。

holding = prices[0]
for i in range(1, len(prices) - 1):
print(holding)
diff_next = 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が1から始まっているので、iとi-1を比べるんだろうなと推測したのでここであれ?となりました。
動かないコードも貼っていただけると読み手にとっていい練習になるのでありがたいです!

Comment on lines +91 to +99
# 1. 谷 (Valley) を見つける
while i < num_prices - 1 and prices[i+1] <= prices[i]:
i += 1
bottom = prices[i]
# 2. 山 (Peak) を見つける
while i < n - 1 and prices[i+1] >= prices[i]:
i += 1
peak = prices[i]
total_profit += peak - bottom
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この辺は個人的には関数化しますかね。
L96のnはどこから来ました?(num_pricesの間違い?)

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.

レビューありがとうございます。
nはタイポです。
確かに関数化するか迷いました。

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.

4 participants