-
Notifications
You must be signed in to change notification settings - Fork 0
121.Best-Time-to-Buy-and-Sell-Stock #35
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
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
121.Best-Time-to-Buy-and-Sell-Stock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # 121. Best Time to Buy and Sell Stock | ||
|
|
||
| - sol1.py: 配列を逆向きにたどり、将来の最大値を覚えておきながら、その日買ったときの利益を計算する | ||
| - sol2.py: 配列を順にたどり、過去の最小値を覚えておきながら、その日に売ったときの利益を計算する | ||
|
|
||
| https://discord.com/channels/1084280443945353267/1206101582861697046/1219181674038820945 | ||
| > prices が空の場合は、何を返すことが想定されているでしょうか。 | ||
|
|
||
| 考えていなかったな | ||
|
|
||
| > Haskell だと、scanl scanl1 の二つの関数があります。scanl1 は、リストの先頭を初期値として与えます。 | ||
| > つまり、scanl1 min prices というのが、そこまでの最小値のリストを返します。 | ||
|
|
||
| https://discord.com/channels/1084280443945353267/1196472827457589338/1196473519689703444 | ||
| > 本質的には、 | ||
| > scanl min でその日までの最安の値。 | ||
| > zipWith (-) で利益。 | ||
| > max を取る。 | ||
|
|
||
| https://github.com/mamo3gr/arai60/blob/121_best-time-to-buy-and-sell-stock/121_best-time-to-buy-and-sell-stock/memo.md | ||
| step1: 解法はほぼ同じだが、初期値をlistの先頭としている + コーナーケースを場合分け | ||
| step2: 上のHaskelのコードをpythonに直したもの。itertools.accumulateを使う | ||
| https://docs.python.org/ja/3/library/itertools.html#itertools.accumulate | ||
| - sol3.py | ||
|
|
||
| https://github.com/naoto-iwase/leetcode/pull/42 | ||
| > どのスタイルに準拠するかによりますが、たとえば Google では import itertools -> itertools.islice といった使い方をします。 | ||
| > Use import statements for packages and modules only, not for individual types, classes, or functions. | ||
|
|
||
| > Python 3.9 以降は built-in list を使うことになっています。 | ||
|
|
||
| https://docs.python.org/3/library/typing.html#typing.List | ||
|
|
||
| https://docs.python.org/3/library/typing.html#typing.List | ||
|
|
||
| > Note that to annotate arguments, it is preferred to use an abstract collection type such as Sequence or Iterable rather than to use list or typing.List. | ||
|
|
||
| > これは list 以外でもそうですが、引数の type hint はより abstract に、返り値はより specific にすることが多いかと思います。 | ||
|
|
||
| 色々知らなかった | ||
|
|
||
| > 別の考え方として、「買う前の状態」「株を持っている状態」「売った状態」の3状態しかないので、それぞれの状態での最大の所持金(スタートを0とする)を考えるというのもあります。 | ||
|
|
||
| なるほど、同じコードでも解釈を変えることができるのか |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_price_in_current_or_future = -float("inf") | ||
| max_profits = [] | ||
| for price in prices[::-1]: | ||
| max_price_in_current_or_future = max(max_price_in_current_or_future, price) | ||
| max_profits.append(max_price_in_current_or_future - price) | ||
|
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. これは、
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. その通りですね。メモリ使用量を削減できます。 |
||
|
|
||
| return max(max_profits) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| max_price = -float("inf") | ||
| max_profit = 0 | ||
| for price in prices[::-1]: | ||
| max_price = max(max_price, price) | ||
| max_profit = max(max_profit, max_price - price) | ||
|
|
||
| return max_profit |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| min_price_in_current_or_past = float("inf") | ||
| max_profits = [] | ||
| for price in prices: | ||
| min_price_in_current_or_past = min(min_price_in_current_or_past, price) | ||
| max_profits.append(price - min_price_in_current_or_past) | ||
|
|
||
| return max(max_profits) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| min_price = float("inf") | ||
| max_profit = 0 | ||
| for price in prices: | ||
| min_price = min(min_price, price) | ||
| max_profit = max(max_profit, price - min_price) | ||
|
|
||
| return max_profit |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import itertools | ||
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| if not prices: | ||
| raise ValueError("The input list is empty") | ||
|
|
||
| min_prices = itertools.accumulate(prices, min) | ||
| max_profit = [ | ||
| price - min_price for (price, min_price) in zip(prices, min_prices) | ||
| ] | ||
| return max(max_profit) |
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.
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.
in_current_or_futureの意味を考えてしまいました。この短さならmax_priceでもいいかもしれません。逆順に見ている文脈もすぐ上にありますし。