Skip to content

514. Paint Fence#11

Open
atmaxstar wants to merge 2 commits into
mainfrom
514
Open

514. Paint Fence#11
atmaxstar wants to merge 2 commits into
mainfrom
514

Conversation

@atmaxstar
Copy link
Copy Markdown
Owner

問題:https://www.lintcode.com/problem/514/
言語: Python3


この解法はO(k^n)であり、nかkの数が大きいときは避けたい解法である。なので次は動的計画法を用いた解法を考えていきたい。
全くわからなかったのでGeeksForGeeksの解法をみてみる。
まずi個までのポストでの3つ以上同じ色が並ばない塗り方の通りをnum_of_ways[i]と定義する。すると、num_of_ways[i]はと定義する。すると、num_of_ways[i-1]の最後のポストの色と違う色を置く通り(num_of_ways[i-1]*(k-1))と、num_of_ways[i-2]の最後のポストの色と違う色を2つ連続でおく通り(num_of_ways[i-2]*(k-1))を足したものとなる。これをコードにしていく。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_of_ways[i-1]の最後のポストの色と違う色を置く通り(num_of_ways[i-1](k-1))と、num_of_ways[i-2]の最後のポストの色と違う色を2つ連続でおく通り(num_of_ways[i-2](k-1))を足したものとなる

これでどうしてnum_of_ways[i]を漏れなく重複なく表せますか?

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.

i番目まで制約を破らないように塗った際に、全てのパターンは以下の2パターンに分けることができます。

  1. i番目, i-1番目の色が違う
  2. i番目, i-1番目の色が同じ

全パターンを求めるためにまず1のパターンを求めます。
num_of_ways[i-1]のパターンの中で、ある色Aで終わる組み合わせはnum_of_ways[i-1] / k個存在していてi番目にA以外の色で塗ればi番目、i-1番目の色が違うパターンになります。そのパターンの数は(num_of_ways[i-1] / k) * (k - 1)個ありそれがk色分存在するためnum_of_ways[i-1] * (k - 1)通りになります。

次にパターン2の数を求めます。
これは i-2番目までのポストの塗り方がそれぞれあって、それらに i-1, i番目に同じ色を2つ連続で置くことで求めることができます。ただし3つ以上同じ色が並ぶことはできないため、i-2番目の色とは違う色を選ぶ必要があります。i-2番目の色に対して選べる色は k - 1 通りなので、num_of_ways[i-2] * (k - 1)通りになります。

これらの2パターンの組み合わせを足して、
num_of_ways[i]= num_of_ways[i-1] * (k - 1) + num_of_ways[i-2] * (k - 1)
となります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ありがとうございます。その通りだと思います。

i番目, i-1番目の色が違う
i番目, i-1番目の色が同じ

これらを変数に置く方法の方が、直感的かもしれません。

```Python
class Solution:
def num_ways(self, n: int, k: int) -> int:
num_of_ways = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
h-masder/Arai60#18 (comment)

if n == 1:
return k

num_of_ways = [0]*n
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
mt2324/leetcode#2 (comment)

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.

配列の時だとそのクセが抜けておりました...
開けるように心がけます。

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.

3 participants