-
Notifications
You must be signed in to change notification settings - Fork 0
276. Paint Fence #33
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 #33
Conversation
|
||
private: | ||
int CountNumWays(int num_posts, const int num_colors, vector<int>& memoization) { | ||
if (memoization[num_posts] >= 0) { |
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.
これでも良いんですが、UNDEFINED = -1
として、memoization[num_posts] != UNDEFINED
のほうが意図が伝わりやすいと思いました
ここで説明したいのって、正の値が入っていることではなくすでに値が決まっていることだと思うので
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.
@Yoshiki-Iwasa
いつもレビューありがとうございます。
ここで説明したいのって、正の値が入っていることではなくすでに値が決まっていることだと思うので
指摘の通りです。今回の場合は、vectorが勝手に初期化する値ではなくundefinedであるのかどうかを判断したかったです。ありがとうございます!
class Solution { | ||
public: | ||
int numWays(int num_posts, int num_colors) { | ||
MyLRUCache cache(1000); |
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.
これ、num_posts が 1000 を超えたらどうなりますかね。
計算時間としては爆発しないようですね。cache サイズが2以上あれば大丈夫ですか?
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.
レビューありがとうございます。今回の問題(自分の実装の場合)の場合は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); |
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.
vector<int>
はヒープ領域にメモリを確保する都合上、スタック領域だけで処理を完結させた場合と比べ、処理が重くなる場合があります。 step2 のように、スタック領域だけで処理を完結することができるのであれば、そちらを選んだほうが良いと思います。
拝見しました。 |
問題へのリンク
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:

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内に各ステップで感じたことを追記します。