Skip to content

Create 276. Paint Fence.md#48

Merged
Mike0121 merged 1 commit intomainfrom
276.-Paint-Fence
Nov 1, 2025
Merged

Create 276. Paint Fence.md#48
Mike0121 merged 1 commit intomainfrom
276.-Paint-Fence

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

LeetCode: https://leetcode.com/problems/paint-fence/description/
LintCode: https://www.lintcode.com/problem/514/description

Description:
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.

Example 1:
Input: n = 3, k = 2
Output: 6
Explanation: All the possibilities are shown.
Note that painting all the posts red or all the posts green is invalid because there cannot be three posts in a row with the same color.

Example 2:
Input: n = 1, k = 1
Output: 1

Example 3:
Input: n = 7, k = 2
Output: 42
 
Constraints:
1 <= n <= 50
1 <= k <= 105
The testcases are generated such that the answer is in the range [0, 231 - 1] for the given n and k.

LeetCode: https://leetcode.com/problems/paint-fence/description/
LintCode: https://www.lintcode.com/problem/514/description

Description:
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.

Example 1:
Input: n = 3, k = 2
Output: 6
Explanation: All the possibilities are shown.
Note that painting all the posts red or all the posts green is invalid because there cannot be three posts in a row with the same color.

Example 2:
Input: n = 1, k = 1
Output: 1

Example 3:
Input: n = 7, k = 2
Output: 42
 
Constraints:
1 <= n <= 50
1 <= k <= 105
The testcases are generated such that the answer is in the range [0, 231 - 1] for the given n and k.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、k を3くらいにして、n=4,5 くらいまで書き出したらいいと思いますよ。

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.

odaさん
ありがとうございます。そうですね、15分以内ではできませんでしたが、解法を見て、n = 4, k = 3の例でやってみて、パターンが見えたので、3回復習時はパターン書いてみるところから書き直しました。

Copy link
Copy Markdown

@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.

良いと思います。

@TORUS0818
Copy link
Copy Markdown

綺麗にまとまっていて読みやすかったです(復習になりました)

必須ではないかもですが、行列表現に関しても軽く確認しておくと良いかもしれません。
https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A3%E3%83%9C%E3%83%8A%E3%83%83%E3%83%81%E6%95%B0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

「1番目、2番目、3番目」という表現が不正確に感じました。現在着目しているポスト、一つ前のポスト、二つ前のポストという意味でしょうか?

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.

@nodchip さん
ありがとうございます。
こちらわかりづらくてすみません。
n = 4、k = 3くらいに設定して、n =1, 2, 3の場合を試しに手で書き出してみる。という意図でした。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

特別な値で初期化すれば、 list() でも書けると思います。

@Mike0121 Mike0121 merged commit 0caacf9 into main Nov 1, 2025
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.

5 participants