-
Notifications
You must be signed in to change notification settings - Fork 0
276. Paint Fence #31
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 #31
Conversation
hayashi-ay
left a comment
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.
すごく気になるところはありませんでした。
| - 行列累乗はnumpyで | ||
| - matrix_power | ||
| - https://numpy.org/doc/2.0/reference/generated/numpy.linalg.matrix_power.html | ||
| - 計算量的には O(n) -> O(log n) と落ちるはずだがleetcode上ではむしろ遅くなった |
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<=50と小さい場合だとnumpyのライブラリ側の処理のオーバーヘッドの方が大きいみたいですね。
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.
import numpy 自体が遅いイメージがありますねえ。
| ```python | ||
| class Solution: | ||
| def numWays(self, n: int, k: int) -> int: | ||
| same = 0 |
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.
個人的には、same_with_previous、different_from_previousくらい書いても良いかなという気もします。
| # initial conditions | ||
| # num_ways_diff[1] = k, num_ways_same[1] = 0 | ||
|
|
||
| num_ways_same[1] = 0 |
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 から始めたのは何か意味があるのでしょうか?通常は 0 から始めることが多いと思います。
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-indexed で考えるようにしてみています。ですが、この辺りまだ模索中です。
| - 変数名は迷ったが、`num_ways_same` と `num_ways_diff` としてみた。`same` と `diff` だけでもいいかもしれない | ||
| ```python | ||
| class Solution: | ||
| def numWays(self, n: int, k: int) -> int: |
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.
引数名をnum_postsとnum_colorsとかにしても良いかと思いました。
|
遅くなりすみません。拝見しました! |
問題へのリンク
leetcode: https://leetcode.com/problems/paint-fence/description/
lintcode: https://www.lintcode.com/problem/514/
Question:
You are painting a fence of n posts with k different colors. You must paint the posts following these rules:
Given the two integers n and k, return the number of ways you can paint the fence.
次に解く問題
300. Longest Increasing Subsequence
README.mdへ頭の中の言語化と記録をしています。