-
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
Conversation
return k; | ||
} | ||
if (n == 2) { | ||
return k * (k - 1) + k; |
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.
例外処理なので単に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 comment
The reason will be displayed to describe this comment to others. Learn more.
kやk * (k - 1) + kが繰り返されているのが少し気になりました。変数にしておいてそれを使うか、あるいはnumWays(1, k)やnumWays(2, k)を呼び出してもいいと思いました。
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.
確かに意味のある名前にしておいたほうがいいですね
- 前に使った色と同じ色でなければよいので、色の数の多さはそこまで影響がない | ||
- 1枚塗るか2枚塗るというのを配列を1,2進むで表現可能 | ||
の二点に気づいたら割とすんなりできた | ||
kを入れる位置に少し悩んだが、なんとかパスした |
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.
「とりあえず入出力が一致すること」が目標になっていませんか。
この練習会を主催できるようにすることを目標にしてください。これは何を言っているかというと、私が感じる程度の平凡なことはだいたい感じるようにする必要があるということです。
なぜこれを言い出したかというと、自分のコードをレビューするのは自分の責務ではない、と考えていると感じましたが、レビューする能力を付けることは目標に入るのですから、どのようなレビューがつくかを予測していないならば、学習の勾配が大きく損なわれています。
if (n == 1) { | ||
return k; | ||
} | ||
vector<long> dp(n+1); |
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.
dp という命名は、何が入っているのか分からないです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l
また、とりあえず long にしてみるという考え方がものすごくよくないです。
int だとあふれる場合があると考えたのならば、単調増加なのだから return するところで困るでしょう。(だから、関数シグネーチャー自体を変えるべきですね。)
しかも、long は大きさが32ビット以上、int 以上なので、int と同じかもしれません。
つまり、long にした意図や意味が合理的に存在しているようには見えないのです。
} | ||
|
||
int previous_count = k; | ||
int count = k * (k - 1) + k; |
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.
ここは単純な式変形で k * k
になると思いますが、なにか特別な意図があるでしょうか。私としては k * k でよいかなと感じます。
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.
2枚目までカウントした状態で、2枚ずつ塗った塗り方のパターン(k
)と1枚ずつ塗ったパターン数(k*(k-1)
)との合算であることを明示したかったのでこのようにしていました。
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 comment
The reason will be displayed to describe this comment to others. Learn more.
問題の制約上ありえないですがn == 0の場合のコードもしくはコメントがあってもいいかなと思いました。
https://leetcode.com/problems/paint-fence/description/
https://www.lintcode.com/problem/514/
Kaichi-Irie/leetcode-python#13
irohafternoon/LeetCode#33
tarinaihitori/leetcode#30
Fuminiton/LeetCode#30
olsen-blue/Arai60#30
katsukii/leetcode#16
Mike0121/LeetCode#48
kazukiii/leetcode#31
Ryotaro25/leetcode_first60#33
Yoshiki-Iwasa/Arai60#44
TORUS0818/leetcode#32