Skip to content

Create 121. Best Time to Buy and Sell Stock.md#37

Open
fuga-98 wants to merge 1 commit into
mainfrom
121.-Best-Time-to-Buy-and-Sell-Stock
Open

Create 121. Best Time to Buy and Sell Stock.md#37
fuga-98 wants to merge 1 commit into
mainfrom
121.-Best-Time-to-Buy-and-Sell-Stock

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented Apr 20, 2025

Copy link
Copy Markdown

@huyfififi huyfififi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コード読みやすくていいと思います 👍


### 思考ログ

Kadaneっぽい感じで。時間O(N) 空間O(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kadane's Algorithmっていうのがあるんですね、初めて知りました 👀

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

知らなくてもよいものらしいです。
前の問題で使ったので知っていました。

```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
assert type(prices) is list
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私もここ業務なら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との比較でいいとも思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

知らなかったので助かります!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duck Typing, type, isinstance の3種類があるということですね。
関係リンクはこれです。
https://discord.com/channels/1084280443945353267/1347375192065703986/1351321684166053918

特にあとは問題ないかと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューありがとうございます!
なるべく型チェックしたほうが良いと思っていましたが、必ずしもそうではないのですね。

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かな。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意です。
コードも読みやすかったです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants