Skip to content

Conversation

@docto-rin
Copy link
Owner


max_profit = 0
min_price = prices[0]
for price in prices[1:]:

Choose a reason for hiding this comment

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

コピーで無駄なメモリを使うというのはそうなのですが、0 から iterate しても問題なく解けますね (問題設定上も同日に売って買うことに対する制限はなかったと思います)。

Copy link
Owner Author

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")とするのが一貫している(無駄がない?)と感じ、そう書きました。

Comment on lines +116 to +121
if price < min_price:
min_price = price
continue
potential = price - min_price
if potential > max_profit:
max_profit = potential

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 による分岐は、minmax で簡略化できるケースがある、という発想があるとよいのではないでしょうか。

Copy link
Owner Author

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

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
Copy link

@ryosuketc ryosuketc Oct 29, 2025

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 で簡便のためこのようにインポートすることは問題ないと思いますが、スタイルによってバリエーションがあることは頭にあると良いかと思います。

Copy link
Owner Author

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:

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 でしたね。

Copy link
Owner Author

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 あたりでヒントをつけるほうがよいだろうと思います。

なので、こういうことになるんですね。

- 時系列に沿って考えるのが自然とみて、最終日を右に広げていくことを考える。
- なお、空売りしてから買うことにすればおそらく右から考えることもできそうだが、特にやる意味はない。
- 少なくとも、部分問題での価格の最小値は必要。逆にそれさえ分かればいけそう。
- 価格の最小値より小さい価格が来たら、買いどきの更新。それ以外は売りどきかだけ判断すれば問題は順次解ける。
Copy link

Choose a reason for hiding this comment

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

別の考え方として、「買う前の状態」「株を持っている状態」「売った状態」の3状態しかないので、それぞれの状態での最大の所持金(スタートを0とする)を考えるというのもあります。

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