-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0121-best-time-to-buy-and-sell-stock.md #42
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?
Conversation
|
|
||
| max_profit = 0 | ||
| min_price = prices[0] | ||
| for price in prices[1:]: |
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.
コピーで無駄なメモリを使うというのはそうなのですが、0 から iterate しても問題なく解けますね (問題設定上も同日に売って買うことに対する制限はなかったと思います)。
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.
ありがとうございます。
おっしゃるとおり、0からiterateも動作上問題ないです。
自分は、min_price = prices[0]を初期値とするなら1からiterateするし、0からiterateするならmin_price = prices = float("inf")とするのが一貫している(無駄がない?)と感じ、そう書きました。
| if price < min_price: | ||
| min_price = price | ||
| continue | ||
| potential = price - min_price | ||
| if potential > max_profit: | ||
| max_profit = potential |
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.
min_price = min(price, min_price)
max_profix = max(price - min_price, max_profit)でよりシンプルに書けます。if による分岐は、min や max で簡略化できるケースがある、という発想があるとよいのではないでしょうか。
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.
ありがとうございます。
そちら、他の参加者の方のコードでも拝見しました。
個人的には、if-continue-ifで書くと、if内の処理は高々一方しかやらない、ということが分かりやすいので好みでした。
しかしながら、maxやminはコードをコンパクトにでき、状況次第では好ましいケースもあると思うので、しっかり頭に留めておきます。
| ### 実装3 | ||
|
|
||
| ```python3 | ||
| from typing import List |
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.
Python 3.9 以降は built-in list を使うことになっています。
https://docs.python.org/3/library/typing.html#typing.List
|
|
||
| ```python3 | ||
| from typing import List | ||
| from itertools import islice |
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.
どのスタイルに準拠するかによりますが、たとえば Google では import itertools -> itertools.islice といった使い方をします。
https://google.github.io/styleguide/pyguide.html#22-imports
LeetCode で簡便のためこのようにインポートすることは問題ないと思いますが、スタイルによってバリエーションがあることは頭にあると良いかと思います。
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.
ありがとうございます。
Use import statements for packages and modules only, not for individual types, classes, or functions.
こちら、初めて知りました。関数やクラスを直接importすると、由来がわからなくなるから好ましくないんですね。勉強になります。
|
|
||
|
|
||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: |
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.
List -> list というのもそうですが、ここはそもそも Sequence あたりでヒントをつけるほうがよいだろうと思います。
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 にすることが多いかと思います。
…と書きはしましたがこれは LeetCode が勝手に生成している signature でしたね。
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.
ありがとうございます。
引数の type hint はより abstract に、返り値はより specific にすることが多いかと思います。
なるほどですね。思い返すとPython documentationもそうなっていますね。
ここはそもそも Sequence あたりでヒントをつけるほうがよいだろうと思います。
なので、こういうことになるんですね。
| - 時系列に沿って考えるのが自然とみて、最終日を右に広げていくことを考える。 | ||
| - なお、空売りしてから買うことにすればおそらく右から考えることもできそうだが、特にやる意味はない。 | ||
| - 少なくとも、部分問題での価格の最小値は必要。逆にそれさえ分かればいけそう。 | ||
| - 価格の最小値より小さい価格が来たら、買いどきの更新。それ以外は売りどきかだけ判断すれば問題は順次解ける。 |
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.
別の考え方として、「買う前の状態」「株を持っている状態」「売った状態」の3状態しかないので、それぞれの状態での最大の所持金(スタートを0とする)を考えるというのもあります。
この問題:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
次の問題:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/