Skip to content

62. Unique Paths#32

Open
TakayaShirai wants to merge 1 commit intomainfrom
62_unique_paths
Open

62. Unique Paths#32
TakayaShirai wants to merge 1 commit intomainfrom
62_unique_paths

Conversation

@TakayaShirai
Copy link
Owner

@TakayaShirai TakayaShirai self-assigned this Feb 26, 2026
Comment on lines +4 to +5
// 数学を使わずにゴリゴリにやるとしたら、全ての経路を通ってから、その経路を set で保存して、その set の長さを返せばいい。
// 全ての経路をしらみつぶしに通るには、O(2^(m+n-2))通りを処理する必要がある。
Copy link

Choose a reason for hiding this comment

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

本題から逸れますが,「→」と「↓」を自由に用いて$m+n-2$文字の文字列を作るという意味でしょうか?
(経路を数えるならしらみ潰しにしても最終的な答えと同じ$\binom{m+n-2}{m-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.

何も考えずに全部の位置で「→」と「↓」にいく選択肢があり、各マスで2通りの進み方があるから 2^(m+n-2) かなと思って書きましたが、特にこの方法でも経路の重複がなさそうなので、確かに最終的な答えと同じになると思います。

Comment on lines +18 to +20
if (num2 > num1) {
throw Exception("num1 should be larger than num2.");
}
Copy link

Choose a reason for hiding this comment

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

せっかくなら,非正整数も弾いてあげると良いかもしれません(本体関数の方で弾いているので問題はないですが再利用することを考えて).

Comment on lines +42 to +44
if (m < 0 || n < 0) {
throw Exception("Both of inputs should be larger than 0.");
}
Copy link

Choose a reason for hiding this comment

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

nit: エラーメッセージに整合的にするにはif (m <= 0 || n <= 0)ですね.

if (num2 > num1) {
throw Exception("num1 should be larger than num2.");
}

Copy link

Choose a reason for hiding this comment

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

私なら例外処理を一つの塊と見て,ここの空行は入れないです.好みの問題でしょう.

uniquePathsCounts[row][col] = 1;
return uniquePathsCounts[row][col]!;
}

Copy link

Choose a reason for hiding this comment

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

私なら3つのif分岐を「再帰呼び出し不要のパターン」として一つの塊と見て,ここの空行は入れないです.好みの問題でしょう.


// 一次元配列の方法も書いておく
class Solution {
int uniquePaths(int m, int n) {
Copy link

Choose a reason for hiding this comment

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

m,nだとわかりづらいため、別の名前をつけても良さそうと思いました。

throw Exception("Both of inputs should be larger than 0.");
}

var uniquePathsCounts = List.generate(m, (_) => List<int?>.filled(n, null));
Copy link

Choose a reason for hiding this comment

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

私がちょっと調べた範囲では、dartの標準では関数のメモ化をサポートする機能は無く、自前でキャッシュを管理しないといけなさそうですね。
サードパーティのライブラリはありそうでしたが…。
https://pub.dev/packages/cached

return uniquePathsCounts[row][col]!;
}

uniquePathsCounts[row][col] = helper(row - 1, col) + helper(row, col - 1);
Copy link

Choose a reason for hiding this comment

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

ヘルパー関数の名前にも親の uniquePaths が入ったほうが、前の結果を積み上げている感じがして好みです。実質的に helperuniquePaths なんですが、それが名前に入っていたほうが頭の中の変換がひとつ省略できるというか。

Suggested change
uniquePathsCounts[row][col] = helper(row - 1, col) + helper(row, col - 1);
uniquePathsCounts[row][col] = uniquePathsHelper(row - 1, col) + uniquePathsHelper(row, col - 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.

4 participants