Create 122. Best Time to Buy and Sell Stock II.md#38
Open
fuga-98 wants to merge 1 commit into
Open
Conversation
oda
reviewed
Apr 22, 2025
Comment on lines
+145
to
+146
| if i == num_prices - 1: | ||
| break |
hroc135
reviewed
Apr 23, 2025
| if i == num_prices - 1: | ||
| break | ||
| profit -= prices[i] | ||
| # 頂点を見つける |
There was a problem hiding this comment.
こういうコメントのおかげもあって、一つの(外側の)ループで1連の株売買をしていることがすぐ読み取れますし、手でやるならこうなると思います。
一方、実務で書くなら前日からの値上がり分を累積していく方法の方がシンプルに書けるので、私はそちらを選択すると思いました。
Owner
Author
There was a problem hiding this comment.
レビューありがとうございます!
実務なら私も累積和を選択すると思います。
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| assert isinstance(prices, list) | ||
| if len(prices) <= 1: |
| result = 0 | ||
| holding = prices[0] | ||
| for i in range(1, len(prices) - 1): | ||
| print(holding) |
There was a problem hiding this comment.
print(f'i: {i}, holding: {holding}')
みたいにログを出すとデバッグに有用かもしれません。多くの情報をログに出すくらいならデバッガーを使った方がいいかもしれませんが。
hroc135
reviewed
Apr 23, 2025
| holding = prices[0] | ||
| for i in range(1, len(prices) - 1): | ||
| print(holding) | ||
| diff_next = prices[i+1] - prices[i] |
There was a problem hiding this comment.
ループ定義でiが1から始まっているので、iとi-1を比べるんだろうなと推測したのでここであれ?となりました。
動かないコードも貼っていただけると読み手にとっていい練習になるのでありがたいです!
nittoco
reviewed
Apr 25, 2025
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 |
There was a problem hiding this comment.
この辺は個人的には関数化しますかね。
L96のnはどこから来ました?(num_pricesの間違い?)
Owner
Author
There was a problem hiding this comment.
レビューありがとうございます。
nはタイポです。
確かに関数化するか迷いました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/