-
Notifications
You must be signed in to change notification settings - Fork 0
276. Paint Fence #24
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 #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## 問題: [276. Paint Fence](https://www.lintcode.com/problem/514/) |
| 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$ | ||
| - $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)$ | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. もう少し、変数の意図が明確になるといいなと思います
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかに |
||
| 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)$ | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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秒 | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
ここは、「$post_{n}$と$post_{n-1}$を同じに塗れる数は、$post_{n-2}$と違う色になっている$post_(n-1)$の数である、ということかと思いましたが,あっていますでしょうか。
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.
コメントありがとうございます。その理解で合っています。