Skip to content

63. Unique Paths II#32

Open
5103246 wants to merge 1 commit intomainfrom
63-unique-paths-ii
Open

63. Unique Paths II#32
5103246 wants to merge 1 commit intomainfrom
63-unique-paths-ii

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Nov 9, 2025

- 空間計算量はO(min(m, n))になる
- スタート地点に障害物があるケースを考えると、DP[0][0]の初期化も障害物があるか確認したほうがよい
- 前回の問題ではnCrで解けたが、今回は無理そう
- 他の解き方が出てこないので、読みやすい二次元DPで解く。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

上から下に見ていって、障害物がない行については、ある行の結果のベクトルに、以下のような行列を掛けてあげることで、結果を求めることができます。

1 1 1 1 1 ...
0 1 1 1 1 ...
0 0 1 1 1 ...
0 0 0 1 1 ...
0 0 0 0 1 ...
...

DP の計算式を行列の掛け算で表しています。
また、障害物がない行が M 行続く場合、行列の M 乗を計算してからベクトルに掛けることで、高速化できます。 N × N の行列の掛け算は O(N^3) で求められるので、 M 乗は Exponentiation by Squaring で O(N^3 log M) で求めることができます。
障害物がある行は DP で求められます。

ソフトウェアエンジニアの常識には入っていないと思います。 Exponentiation by Squaring の部分は、単体では常識に含まれていると思います。

Comment on lines +88 to +90
if (column > 0) {
num_paths[column] += num_paths[column - 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.

自分はif column == 0: continueで弾いてからnum_paths[column] += num_paths[column - 1]と書きたくなるんですが、好みの範囲だと思います。

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