Skip to content

276. Paint Fence #16

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

276. Paint Fence #16

wants to merge 1 commit into from

Conversation

katsukii
Copy link
Owner

@katsukii katsukii commented Feb 6, 2025

問題

https://leetcode.com/problems/paint-fence/description/

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.

言語

Java

次に解く問題

22. Generate Parentheses

Comment on lines +130 to +131
long dp1 = k * k; // dp(2)
long dp2 = k; // dp(1)
Copy link

Choose a reason for hiding this comment

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

この dp、dynamic programming でさえない気がしますね。

long dp1 = k * k; // dp(2)
long dp2 = k; // dp(1)

return (int)(result[0][0] * dp1 + result[0][1] * dp2);
Copy link

Choose a reason for hiding this comment

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

全体 int でいいのではないでしょうか。意味が分かっているならばともかく「とりあえず long」みたいなのはあまり好まれないでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

お手本を写経しながら練習していたため深く考えずこうなっておりました。以後気をつけます。

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.

2 participants