Create 122. Best Time to Buy and Sell Stock II.md#38
Open
Conversation
Fuminiton
reviewed
Jun 29, 2025
| for i in range(len(prices) - 1): | ||
| if prices[i] < prices[i + 1]: | ||
| max_profit += prices[i + 1] - prices[i] | ||
| return max_profit |
There was a problem hiding this comment.
特に問題ないと思います。
個人的には i を day に変えると、より直感的になるかと思っています。
Owner
Author
There was a problem hiding this comment.
day は自分の選択肢にはなかったので今後は選択肢に入れるようにします。ありがとうございます。
Mike0121
reviewed
Jun 29, 2025
| max_profit = 0 | ||
| index = 0 | ||
| while index < len(prices): | ||
| valley_index = find_next_lowest(index) |
There was a problem hiding this comment.
peakの対義語として、bottomでも良いかもしれません。
また、関数名で使われているので、そのままlowest, highestでも良いかと思いました。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。確かに関数名と合わせた方がわかりやすいですね。
Mike0121
reviewed
Jun 29, 2025
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/hayashi-ay/leetcode/pull/56/files | ||
| - https://github.com/olsen-blue/Arai60/pull/38/files | ||
| - 株を持っている/いない場合の保有現金最大値を管理する方法 |
There was a problem hiding this comment.
DPでも(個人的に少し直感的ではないですが)解けるので、解いてみても良いかと思いました。
Owner
Author
There was a problem hiding this comment.
DP で書いてみました。私もあまり直観的ではないなと感じています。
class Solution:
def maxProfit(self, prices: List[int]) -> int:
cash_with_stock = -prices[0]
cash_without_stock = 0
for i in range(1, len(prices)):
prev_cash_with_stock = cash_with_stock
cash_with_stock = max(cash_with_stock, cash_without_stock - prices[i])
cash_without_stock = max(cash_without_stock, prev_cash_with_stock + prices[i])
return cash_without_stock
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.
This Problem
122. Best Time to Buy and Sell Stock II
Next Ploblem
139. Word Break
言語: Python