Solved Arai60/122. Best Time to Buy and Sell Stock II#38
Open
Solved Arai60/122. Best Time to Buy and Sell Stock II#38
Conversation
…t Time to Buy and Sell Stock II.md
Fuminiton
reviewed
Aug 5, 2025
| for i in range(1, len(prices)): | ||
| price_diff = prices[i] - prices[i-1] | ||
| if price_diff > 0: | ||
| profit += price_diff |
There was a problem hiding this comment.
いいと思いました。
完全に趣味ですが、price_diffよりはprofitの方が好みでした。差に関する変数だという意味にプラスして、当日-前日なのか、前日-当日なのかが伝わる気がするので。
Owner
Author
There was a problem hiding this comment.
確かにそういう考え方もありますね、ありがとうございます!
Mike0121
reviewed
Aug 24, 2025
|
|
||
| - ある点までの利益とその後の利益を足す? | ||
| - 何回も足す必要があるしO(n)を何回かやることになる | ||
| - よく考えると上がるところを全部計上すれば良いだけ |
There was a problem hiding this comment.
これに気づけると楽ですよね。自分は、この手の少し算数的(貪欲法)な問題は各パターンを手書きして考えてみています。
Mike0121
reviewed
Aug 24, 2025
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| profit = 0 | ||
| buy = prices[0] |
There was a problem hiding this comment.
変数名、buyは、動詞単体のため少しわかりづらいと感じました。buy_priceなどはどうでしょうか?
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/