Skip to content

Conversation

@kazukiii
Copy link
Owner

@kazukiii kazukiii commented Sep 30, 2024

問題へのリンク
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:

  • Every post must be painted exactly one color.
  • There cannot be three or more consecutive posts with the same color.
    Given the two integers n and k, return the number of ways you can paint the fence.

次に解く問題
300. Longest Increasing Subsequence

README.mdへ頭の中の言語化と記録をしています。

@kazukiii kazukiii changed the title step1, step2, step3を追加 276. Paint Fence Sep 30, 2024
Copy link

@hayashi-ay hayashi-ay left a 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上ではむしろ遅くなった

Choose a reason for hiding this comment

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

計算量は定数倍を無視した極限での振る舞いを表すものなので、計算量的に良くても実際の処理速度が早いかどうかはまた別の話ですね。今回みたいにn<=50と小さい場合だとnumpyのライブラリ側の処理のオーバーヘッドの方が大きいみたいですね。

Copy link

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

Choose a reason for hiding this comment

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

個人的には、same_with_previousdifferent_from_previousくらい書いても良いかなという気もします。

# initial conditions
# num_ways_diff[1] = k, num_ways_same[1] = 0

num_ways_same[1] = 0
Copy link

Choose a reason for hiding this comment

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

インデックスを 1 から始めたのは何か意味があるのでしょうか?通常は 0 から始めることが多いと思います。

Copy link
Owner Author

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:

Choose a reason for hiding this comment

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

引数名をnum_postsnum_colorsとかにしても良いかと思いました。

@TORUS0818
Copy link

遅くなりすみません。拝見しました!
良いと思います。

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.

7 participants