-
Notifications
You must be signed in to change notification settings - Fork 0
Create 121. Best Time to Buy and Sell Stock.md #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # 進め方 | ||
|
|
||
| Step1 : 問題を解く。 | ||
|
|
||
| Step2 : 他の人のPRを参照し、コメントする。 | ||
|
|
||
| Step3 : 3回続けてエラーが出ないように書く。ドキュメントを参照する。 | ||
|
|
||
| # 実践 | ||
|
|
||
| ## Step1 | ||
|
|
||
| ### 思考ログ | ||
|
|
||
| Kadaneっぽい感じで。時間O(N) 空間O(1) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| assert type(prices) is list | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 私もここ業務なら nitpickingですが、typeを見るときは
switowski.com - type() vs. isinstance()
Pythonはduck typingなので、
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 知らなかったので助かります! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duck Typing, type, isinstance の3種類があるということですね。 特にあとは問題ないかと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. レビューありがとうございます! DuckTypingについて |
||
| max_profit = 0 | ||
| min_value = prices[0] | ||
| for i in range(1, len(prices)): | ||
| min_value = min(min_value, prices[i]) | ||
| candidate = prices[i] - min_value | ||
| max_profit = max(max_profit, candidate) | ||
| return max_profit | ||
| ``` | ||
|
|
||
| ## Step2 | ||
|
|
||
| ### 同じ問題を解いた人のプルリクを見る | ||
|
|
||
| https://github.com/huyfififi/coding-challenges/pull/4/files | ||
|
|
||
| - 二重ループ、配列を使った方法がある。 | ||
| - 思いついたが、検討をしなかったのはよくない気がした。 | ||
|
|
||
| https://github.com/olsen-blue/Arai60/pull/37 | ||
|
|
||
| - https://docs.python.org/3/library/itertools.html#itertools.accumulate | ||
| - 累積和に関するメソッドが存在する | ||
|
|
||
| https://github.com/hroc135/leetcode/pull/35/files | ||
|
|
||
| - 後ろから見ていく方法。このときは最大値を保持しておく | ||
|
|
||
| https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.8qw2um7il4s5 | ||
|
|
||
| - Listが空なら何を返すか。ユーザーフェイシングなら0かな。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同意です。 |
||
|
|
||
| ## Step3 | ||
|
|
||
| ### 3回連続で再現できるようになる | ||
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| if not prices or len(prices) <= 1: | ||
| return 0 | ||
| max_profit = 0 | ||
| min_price = math.inf | ||
| for price in prices: | ||
| max_profit = max(max_profit, price - min_price) | ||
| min_price = min(min_price, price) | ||
| return max_profit | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kadane's Algorithmっていうのがあるんですね、初めて知りました 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
知らなくてもよいものらしいです。
前の問題で使ったので知っていました。