Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions 121. Best Time to Buy and Sell Stock.md
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)
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

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

Choose a reason for hiding this comment

The 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
```