Skip to content

Conversation

Ryotaro25
Copy link
Owner

@Ryotaro25 Ryotaro25 commented Aug 31, 2024

問題へのリンク
https://leetcode.com/problems/paint-fence/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:
Screenshot 2024-08-31 at 16 31 04

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.

備考

次に解く問題の予告
Longest Increasing Subsequence

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、top_down.cpp、LRU.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。


private:
int CountNumWays(int num_posts, const int num_colors, vector<int>& memoization) {
if (memoization[num_posts] >= 0) {

Choose a reason for hiding this comment

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

これでも良いんですが、UNDEFINED = -1として、memoization[num_posts] != UNDEFINEDのほうが意図が伝わりやすいと思いました

ここで説明したいのって、正の値が入っていることではなくすでに値が決まっていることだと思うので

Copy link
Owner Author

Choose a reason for hiding this comment

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

@Yoshiki-Iwasa
いつもレビューありがとうございます。

ここで説明したいのって、正の値が入っていることではなくすでに値が決まっていることだと思うので

指摘の通りです。今回の場合は、vectorが勝手に初期化する値ではなくundefinedであるのかどうかを判断したかったです。ありがとうございます!

class Solution {
public:
int numWays(int num_posts, int num_colors) {
MyLRUCache cache(1000);
Copy link

Choose a reason for hiding this comment

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

これ、num_posts が 1000 を超えたらどうなりますかね。

計算時間としては爆発しないようですね。cache サイズが2以上あれば大丈夫ですか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda

レビューありがとうございます。今回の問題(自分の実装の場合)の場合は2で問題なかったです。
皆さんのを写経するだけで、cacheを1000にする理由を考えておりませんでした。

関数CountNumWaysの中で、使うのは今いる箇所から-1したところと-2した箇所の情報だけです。

int same_pattern_ways = CountNumWays(num_posts - 2, num_colors, cache);
int different_pattern_ways = CountNumWays(num_posts - 1, num_colors, cache);

PutしていてもCacheのサイズ2つ確保しておけば必要な分は消されないので問題なさそうです。
自分の解法だとnum_postsの数はあまり影響しないので、使えてなさそうですね。。。

return num_colors * num_colors;
}

vector<int> past_two_colors(2);
Copy link

Choose a reason for hiding this comment

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

vector<int> はヒープ領域にメモリを確保する都合上、スタック領域だけで処理を完結させた場合と比べ、処理が重くなる場合があります。 step2 のように、スタック領域だけで処理を完結することができるのであれば、そちらを選んだほうが良いと思います。

@TORUS0818
Copy link

拝見しました。
コメント以外は良いと思いました。

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