Create 121. Best Time to Buy and Sell Stock.md#37
Conversation
|
|
||
| ### 思考ログ | ||
|
|
||
| Kadaneっぽい感じで。時間O(N) 空間O(1) |
There was a problem hiding this comment.
Kadane's Algorithmっていうのがあるんですね、初めて知りました 👀
There was a problem hiding this comment.
知らなくてもよいものらしいです。
前の問題で使ったので知っていました。
| ```python | ||
| class Solution: | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| assert type(prices) is list |
There was a problem hiding this comment.
私もここ業務ならassertで落とすなと思いました。
nitpickingですが、typeを見るときはisinstance()を使う方が好まれることもあるようです。
PEP8
Object type comparisons should always use isinstance() instead of comparing types directly:
switowski.com - type() vs. isinstance()
isinstance is usually the preferred way to compare types. It's not only faster but also considers inheritance, which is often the desired behavior. In Python, you usually want to check if a given object behaves like a string or a list, not necessarily if it's exactly a string.
Pythonはduck typingなので、listの挙動をしてさえいれば実態はなんでもいいし、そういったインプットを受け入れるのがPythonicである、と指摘された記憶があります。
が、今回の場合は本当にlist以外を受け付けるつもりはないのでlistとの比較でいいとも思います。
There was a problem hiding this comment.
Duck Typing, type, isinstance の3種類があるということですね。
関係リンクはこれです。
https://discord.com/channels/1084280443945353267/1347375192065703986/1351321684166053918
特にあとは問題ないかと思います。
There was a problem hiding this comment.
レビューありがとうございます!
なるべく型チェックしたほうが良いと思っていましたが、必ずしもそうではないのですね。
DuckTypingについて
https://docs.python.org/ja/3.13/library/functions.html#hasattr
https://docs.python.org/3/glossary.html#term-duck-typing
|
|
||
| https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.8qw2um7il4s5 | ||
|
|
||
| - Listが空なら何を返すか。ユーザーフェイシングなら0かな。 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/