-
Notifications
You must be signed in to change notification settings - Fork 0
276. Paint Fence #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
276. Paint Fence #44
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
|
|
||
| Solve Time: 20:00 | ||
|
|
||
| Time: O(n) | ||
| Space: O(n) | ||
|
|
||
| 色の数で混乱しかけたが | ||
| - 前に使った色と同じ色でなければよいので、色の数の多さはそこまで影響がない | ||
| - 1枚塗るか2枚塗るというのを配列を1,2進むで表現可能 | ||
| の二点に気づいたら割とすんなりできた | ||
| kを入れる位置に少し悩んだが、なんとかパスした | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| int numWays(int n, int k) { | ||
| if (k == 1 && n >= 3) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 問題の制約上ありえないですがn == 0の場合のコードもしくはコメントがあってもいいかなと思いました。 |
||
| return 0; | ||
| } | ||
| if (n == 1 || k == 1) { | ||
| return k; | ||
| } | ||
| vector<long> dp(n + 1); | ||
| dp[1] = k; | ||
| dp[2] = k; | ||
| for (int i = 2; i <= n; ++i) { | ||
| dp[i] += dp[i - 1] * (k - 1); | ||
| if (i >= 2) { | ||
| dp[i] += dp[i - 2] * (k - 1); | ||
| } | ||
| } | ||
| return dp[n]; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Solution { | ||
| public: | ||
| int numWays(int n, int k) { | ||
| if (k == 1) { | ||
| if (n>=3) { | ||
| return 0; | ||
| } | ||
| return 1; | ||
| } | ||
| if (n == 1) { | ||
| return k; | ||
| } | ||
| vector<long> dp(n+1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dp という命名は、何が入っているのか分からないです。 また、とりあえず long にしてみるという考え方がものすごくよくないです。 |
||
| dp[1] = k; | ||
| dp[2] = k; | ||
| for (int i = 2; i <= n; ++i) { | ||
| dp[i] += dp[i - 1] * (k - 1); | ||
| if (i > 2) { | ||
| dp[i] += dp[i - 2] * (k - 1); | ||
| } | ||
| } | ||
| return dp[n]; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Solution { | ||
| public: | ||
| int numWays(int n, int k) { | ||
| if (n == 1) { | ||
| return k; | ||
| } | ||
| if (n == 2) { | ||
| return k * (k - 1) + k; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 例外処理なので単にk * kあるいはk ** 2でも良いと思いました。 |
||
| } | ||
|
|
||
| int previous_count = k; | ||
| int count = k * (k - 1) + k; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kやk * (k - 1) + kが繰り返されているのが少し気になりました。変数にしておいてそれを使うか、あるいはnumWays(1, k)やnumWays(2, k)を呼び出してもいいと思いました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かに意味のある名前にしておいたほうがいいですね There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここは単純な式変形で
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2枚目までカウントした状態で、2枚ずつ塗った塗り方のパターン( |
||
| for (int i = 2; i < n; ++i) { | ||
| int tmp = count; | ||
| count = count * (k - 1) + previous_count * (k - 1); | ||
| previous_count = tmp; | ||
| } | ||
| return count; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「とりあえず入出力が一致すること」が目標になっていませんか。
この練習会を主催できるようにすることを目標にしてください。これは何を言っているかというと、私が感じる程度の平凡なことはだいたい感じるようにする必要があるということです。
なぜこれを言い出したかというと、自分のコードをレビューするのは自分の責務ではない、と考えていると感じましたが、レビューする能力を付けることは目標に入るのですから、どのようなレビューがつくかを予測していないならば、学習の勾配が大きく損なわれています。