Skip to content

Create 209. Minimum Size Subarray Sum.md#48

Open
fuga-98 wants to merge 1 commit into
mainfrom
209.-Minimum-Size-Subarray-Sum
Open

Create 209. Minimum Size Subarray Sum.md#48
fuga-98 wants to merge 1 commit into
mainfrom
209.-Minimum-Size-Subarray-Sum

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 18, 2025

https://github.com/olsen-blue/Arai60/pull/50/files

- for で right 固定して、while で left を削るイメージ 同じ
- 番兵はどうするか。math.infで良いと感じてしまう。型違うけど、それはそれでおかしな実装をしたときに気づける気もする。
Copy link
Copy Markdown

@olsen-blue olsen-blue May 18, 2025

Choose a reason for hiding this comment

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

目的としては、プログラムがこれから最小値を探索していくために、明らかに大きすぎる異物みたいなのを初期値として設定できれば良いので、math.infでも良いと思います。
一部、型に配慮したい人もいるみたいなので、状況に応じてsys.maxsizeとかに切り替える柔軟さがあれば良いのかもしれません。

Comment on lines +135 to +136
for base in range(len(prefix_sum)):
desired = target + prefix_sum[base]
Copy link
Copy Markdown

@olsen-blue olsen-blue May 18, 2025

Choose a reason for hiding this comment

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

baseは、若干抽象的な気がしました。from_indexとかでどうでしょうか。
desiredも、target_prefix_sumとかですかね、ちょっと長いですがtargetと差別化したいですよね。

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.

レビューありがとうございます。
いつも参考にさせていただいています。
意図としては
from_indexは閉区間ぽさを感じて変えてみました。
desiredはスコープ短いので、変数で説明するより、中身を読むことを期待しました。

でもおっしゃる通り、改善の余地がありそうです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コメントを書くという選択肢もあります。

ちなみに、コードを読んでもらう負荷をなるべく下げる工夫をすることを推奨されていると自分は理解しています。
https://discord.com/channels/1084280443945353267/1233603535862628432/1235977874528538674


```

線形
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

累積和をループしながら構築することもできますが、この解法のように事前に累積和を求めておくことで、メインのループの中身のコードがいくらかシンプルになるというメリットはあるかもしれませんね。

if not nums:
return 0
if target <= 0:
return 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.

これ return 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.

あ、target 以上になればいいんですね。失礼しました。

return min_length
```

開区間を使うと処理がシンプルにかけるが、prefix_sumの最初を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.

開区間と呼ぶならば、「何」についての区間を考えているかをはっきりさせたほうがいいです。
どのような区間(つまり不等式で作られる範囲)と思って開と呼んでいますか。

私は半開区間と呼んでもいいかなと思います。

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.

lo < x の部分だけを見て開区間と書いていました。
でも言われてみれば、lo < x <= hi なので半開区間でした。

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