Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions 31-45/31.Unique-Paths/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#if __has_include("../debug.hpp")
#include "../debug.hpp"
#endif
// ここまでローカルでのデバッグ用なので気にしないでください --------------------

#include <vector>

using namespace std;

// <時間>
// 10分
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

10分で書けているなら、この問題はいいでしょう。

CPython の comb のコードを読んでいる人もいました。

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.

ありがとうございます!
この方などですね
nittoco/leetcode#26

// <感想>
// 最初計算量の見積もりをミスしていて再帰で書いていた。
// 途中で間違いに気づき、メモ化再帰→2次元DPみたいな思考の流れ。
// しばらく競プロ等をしていなかったのでDPを書くのに手こずった。
// なまるのが早いなと思う。(10分で書けたのが驚き)
// <コメント>
// - DPを競プロでしか書いたことがなく、
// エンジニアにとってなじみのあるパターンが何か分からないので
// 他の人のレビューを見てみる
// - 見てみたが大して差があるように感じないのでこのまま完成とする
// - 一応いくつか気になる点があるが、
// この例では質問するのに適切でないと感じるのでとりあえず次に進む
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> num_paths(m, vector<int>(n));
for (int i = 0; i < m; ++i) num_paths[i][0] = 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.

私はぶら下がりは避けますね。

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.

僕はこちらで行かせていただきます(チームの時はチームに従います)
https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

For historical reasons, we allow one exception to the above rules: the curly braces for the controlled statement or the line breaks inside the curly braces may be omitted if as a result the entire statement appears on either a single line (in which case there is a space between the closing parenthesis and the controlled statement) or on two lines (in which case there is a line break after the closing parenthesis and there are no braces).

// OK - braces are optional in this case.
if (x == kFoo) return new Foo();

Copy link
Copy Markdown
Owner Author

@philip82148 philip82148 Feb 7, 2025

Choose a reason for hiding this comment

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

ちなみに↑のHistorical reasonsってどういう意味か聞いてもよいですか?
昔はぶら下がりifがたくさん使われてて、それを修正するのが大変だから、ぐらいの意味ですか?
(そして現代だとワンライナーぶら下がりifを新しく作るのはあまり主流ではない、という)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

書いた人に聞かないと分かりません。どっかのメーリングリストでやりとりがおこなわれておりました。

ただ、昔、慣習的に使われていた、(から好ましくないが慣れて読める人も多いので許す)くらいの温度感でしょう。

現代的な言語、たとえば Rust や Go や Swift などは {} が必要です。いらないのは C C++ Java などですね。

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.

なるほどです。ありがとうございます!

for (int i = 0; i < n; ++i) num_paths[0][i] = 1;
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1];
}
}
return num_paths[m - 1][n - 1];
}
};