Skip to content

Conversation

@TORUS0818
Copy link
Owner

思考ログ:
- 1個前までのmaximum_subarrayから更に伸ばした方がいいか、一旦切るか考える
- 最初```maximum_subarray_so_far[-1]```を反射的に返してしまった
- 配列の要素の意味をちゃんと確認すること
Copy link

Choose a reason for hiding this comment

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

一応、ここの意味を自然言語で説明してくれませんか。
「maximum_subarray_so_far[i] の意味は nums[i] が和として使用する最後の数であった場合、連続する配列の和の最大値」ですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
変数名の情報だけでは不十分でした。コメントを残すように心がけます。

nums[i]が最後の要素になるような部分列集合を全て考え、各部分列の和をそれぞれ取って、そのうち最大のものを要素とする配列、みたいな感じですかね。日本語が難しい、、

maximum_subarray_so_far[i] = max {Σ_k (A_ij[k])} (∀ j)

A:対象とする部分列の配列
i : nums[i]と対応、対象とする部分列集合の最後の要素 = nums[i]
j : 対象とする部分列集合のインデックス
k : 対象とする部分列の要素のインデックス

Copy link

Choose a reason for hiding this comment

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

あー、いや、コメントが必要だというよりは、言語化しておいたほうが理解が深まるだろうと思ったので書いておいたくらいです。

maximum_subarray_sum = nums[0]
prefix_sum = 0
for num in nums:
prefix_sum = max(prefix_sum, 0) + num
Copy link

Choose a reason for hiding this comment

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

このアルゴリズムも prefix_sum が「num を配列の最後の値として使うことに決めた場合の、連続する配列の和の最大値」という意味だと思うと理解できますね。

再帰
```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:

Choose a reason for hiding this comment

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

こちらの解法よりstep1のボトムアップやや累積和がいいと思いました。
関数から2つの値が返ってくることが処理を追い辛くしていると感じました。

一方でこの問題は他に比べて再帰で解くのは難しいと思いました。。。

```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maximum_subarray_sum = -sys.maxsize

Choose a reason for hiding this comment

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

sys.maxsizeを使っているのが少し気になりました。他の言語と違ってPythonのintは制限がないので、sys.maxsize以下の値も取りうるのが個人的にうーんと思った点なのかなと思います。まあ何かしら下限値を用意する必要がありはするので、別に問題はないと思います。

あとはnums[0]を初期にして、forループはnums[1:]について回してあげるのもありかなと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

  • 問題の制約から下限値を自分で定義する
  • np.inf

あたりの選択肢で揺れてますが、今回はこれにしました(intにしておきたい、くらいで深い意味はないです)

return maximum_subarray_sum
```
思考ログ:
- 配列が必要かどうか、は考えておきたい(DPだと反射的に配列を用意しがち)

Choose a reason for hiding this comment

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

DPだと反射的に配列を用意しがちという表現になんかちょっと違和感を感じました。現在の状態の値がひとつ以上前の状態の値によって決まるような問題を解くのに適しているのがDPで、各状態についての値を保存するために配列を用意するという方法がまずあり、工夫すれば配列を用意しないでも書けるくらいの話かなと思います

Copy link
Owner Author

@TORUS0818 TORUS0818 Nov 9, 2024

Choose a reason for hiding this comment

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

DPだと反射的に配列を用意しがち

という自省メモのつもりでした。

ご指摘のとおり、DPの性質上、配列をよく使うという話なのですが、DPだ->配列用意しよう、のような脳死ムーブを(私が)しがちな気がしたので。

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.

5 participants