Skip to content

542. 01 Matrix - #73

Open
naoto-iwase wants to merge 1 commit into
mainfrom
0542-01-matrix
Open

542. 01 Matrix#73
naoto-iwase wants to merge 1 commit into
mainfrom
0542-01-matrix

Conversation

@naoto-iwase

Copy link
Copy Markdown
Owner

542. 01 Matrix


Next: 973. K Closest Points to Origin


num_rows = len(mat)
num_cols = len(mat[0])
INF = num_rows + num_cols

@huyfififi huyfififi Dec 20, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この変数はinfiniteではなくINFと名付けるのはやや誤解を招くので、max_distなどの方が意図が伝わりやすい気がします。
INFという変数名をみて個人的に気になったのは

  • math.inf / float("inf")を使用しない理由は何だろう
  • infiniteという名付けだけど、実際はこの変数の値を超える変数が作れるな、どこかに落とし穴はないだろうか

という点です。

また、固定の数字 (constant) という意味で大文字を使用されているものと予想しますが、関数への入力によって変化する数字なのでconstantではなく、この場合は小文字を使用するパターンの方をよく見るかなと思います。詳しくconstantの定義は調べていませんが...

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.

ありがとうございます。

命名、大文字、どちらも同意です。
max_dist, max_distanceなどが良さそうですね。

### 実装3

- アルゴリズムの検討
- 移動方向を制限してTwo-passでやる方法は定数倍で速そう。

@huyfififi huyfififi Dec 20, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ただの感想ですが、この解法は万人が思いつき、腑に落ちて、どこかに問題が発生した時にロジックを修正できるものではないかなと思っています。(ちょっと魔法?っぽい?)
私がもし業務で似たような課題を与えられたら、大体現実的にとり得る入力サイズを確認して、実行時間に問題がなさそうならBFSを選択すると思います。愚直な方針の方が、長期的にメンテナンスしやすいかなと。(この辺の匙加減は私も調整中なので、感想まで)

DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
result = [[UNVISITED] * num_cols for _ in range(num_rows)]

def is_to_visit(row, col):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら is_visitable() と名付けると思いますが、趣味の範囲だと思います。英語句として自然かどうか自信ありません。

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.

3 participants