Open
Conversation
nodchip
reviewed
Nov 10, 2025
| - 空間計算量はO(min(m, n))になる | ||
| - スタート地点に障害物があるケースを考えると、DP[0][0]の初期化も障害物があるか確認したほうがよい | ||
| - 前回の問題ではnCrで解けたが、今回は無理そう | ||
| - 他の解き方が出てこないので、読みやすい二次元DPで解く。 |
There was a problem hiding this comment.
上から下に見ていって、障害物がない行については、ある行の結果のベクトルに、以下のような行列を掛けてあげることで、結果を求めることができます。
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 の部分は、単体では常識に含まれていると思います。
naoto-iwase
reviewed
Nov 11, 2025
Comment on lines
+88
to
+90
| if (column > 0) { | ||
| num_paths[column] += num_paths[column - 1]; | ||
| } |
There was a problem hiding this comment.
自分はif column == 0: continueで弾いてからnum_paths[column] += num_paths[column - 1]と書きたくなるんですが、好みの範囲だと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
https://leetcode.com/problems/unique-paths-ii/
次の問題
https://leetcode.com/problems/house-robber/