Skip to content
Open
Show file tree
Hide file tree
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
Binary file added 276_paint_fence/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions 276_paint_fence/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [276. Paint Fence](https://www.lintcode.com/problem/514/)
33 changes: 33 additions & 0 deletions 276_paint_fence/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Step 1

- 3連続同じ色にはできない
- $post_{k}$に塗れる色は、$post_{k - 1}$、$post_{k - 2}$による
- 解けなかったので[解説](https://www.geeksforgeeks.org/dsa/painting-fence-algorithm/)を見た
- 塗り方の選択肢は2つ
1. $post_{n}$を$post_{n-1}$と違う色にする
- 色の選択肢は$k-1$
2. $post_{n}$と$post_{n-1}$を同じにする
- 色の選択肢は$k-1$
Copy link
Copy Markdown

@h-masder h-masder May 5, 2026

Choose a reason for hiding this comment

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

ここは、「$post_{n}$と$post_{n-1}$を同じに塗れる数は、$post_{n-2}$と違う色になっている$post_(n-1)$の数である、ということかと思いましたが,あっていますでしょうか。

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.

コメントありがとうございます。その理解で合っています。

- $numWays(n) = numWays(n - 2) * (k - 1) + numWays(n-1)*(k-1)$

```python
class Solution:
"""
@param n: non-negative integer, n posts
@param k: non-negative integer, k colors
@return: an integer, the total number of ways
"""
def num_ways(self, n: int, k: int) -> int:
# write your code here
if n == 0:
return 0
if n == 1:
return k
if n == 2:
return k * k
return num_ways(n - 1) * (k - 1) + num_ways(n - 2) * (k - 1)
```

時間計算量: $O(2^n)$

空間計算量:$O(n)$
33 changes: 33 additions & 0 deletions 276_paint_fence/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Step 2

- 再帰をなくして計算量を改善する

```python
class Solution:
"""
@param n: non-negative integer, n posts
@param k: non-negative integer, k colors
@return: an integer, the total number of ways
"""
def num_ways(self, n: int, k: int) -> int:
# write your code here
if n == 0:
return 0
if n == 1:
return k
if n == 2:
return k * k
num_ways_prev_2 = k
Copy link
Copy Markdown

@h-masder h-masder May 5, 2026

Choose a reason for hiding this comment

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

もう少し、変数の意図が明確になるといいなと思います
num_ways_up_to_two_posts_before などはいかがでしょうか

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.

たしかにnum_ways_prev_2ややわかりづらいかもしれません。
num_ways_up_to_two_posts_before の方がより明確だと思います。

num_ways_prev_1 = k * k

for i in range(2, n):
tmp = num_ways_prev_1
num_ways_prev_1 = (k - 1) * (num_ways_prev_1 + num_ways_prev_2)
num_ways_prev_2 = tmp
return num_ways_prev_1

```

時間計算量: $O(n)$

空間計算量: $O(1)$
32 changes: 32 additions & 0 deletions 276_paint_fence/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Step 3

```python
class Solution:
"""
@param n: non-negative integer, n posts
@param k: non-negative integer, k colors
@return: an integer, the total number of ways
"""
def num_ways(self, n: int, k: int) -> int:
# write your code here
if n == 0:
return 0
if n == 1:
return k
if n == 2:
return k * k
num_ways_prev_2 = k
num_ways_prev_1 = k * k

for i in range(2, n):
tmp = num_ways_prev_1
num_ways_prev_1 = (k - 1) * (num_ways_prev_1 + num_ways_prev_2)
num_ways_prev_2 = tmp
Comment on lines +22 to +24
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tmpは意味がなくて読みにくいので、num_ways_hereで、今のポストまでのパターンを計算しておいて、num_ways_prev2に1を代入、1にhereを代入する、と書くとわかりやすいと思いました。

return num_ways_prev_1
```

1回目: 1分 17秒

2回目: 1分 20秒

3回目: 1分 16秒