Skip to main content

Dynamic Programming - Hard Level - Question 2

Dynamic Programming - Hard Level - Question 2


Leetcode 1931 Painting a Grid with Three Different Colors

You are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted.

Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 10^9 + 7.

Constraints:

1 <= m <= 5

1 <= n <= 1000


Analysis:

One of the difficulties is how to define the state.

In the process of defining a state, we need to consider two questions:

1. is the state defined a full description of the problem asked?

2. how can we derive the transition formula from the previous calculated states?

For this question, it is a matrix, so a natural choice is to use the index i and j. But this is not enough, since at each position we have three choices, (and its color should be different from its neighbors). So a third parameter is needed, and we need to somehow memorize the color information of its neighbors.

To this question, a hint is that the value of m is very small, so we can use `bit-wise` trick to record the color information with a mask, and the base is 3 (since we have 3 different choices for each position: 0 is the first color, 1 is the second, and 2 is the third). And we only need to record the previous m positions, since all the previous ones before the previous n positions have no limit for the remaining states.

To sum up, we can define a state as f(i, j, k), where i and j are the indexes, and k is the color state of the previous m positions. For each position, there are 3 choices, so m positions have pow(3, n) choices in total. For each k, `the most significant bit` is the one from the current position.

To easier coding (or thinking), I decide to rotate the matrix by 90 degree, or swap the m and n. Then we can think about this question line by line (if no swap, it is column by column).

Let focus on the transition formula:

if j == 0

then all the previous n positions are on the previous row. The only position that cannot having the same color as the current position is the one on right top of the current one (i-1, j);

if j > 0

then all the previous n positions are on the both the current row & previous row. There are two positions that cannot have the same color as the current one: one is on the right top of the current one (i-1, j), and the other is the one on the left (i, j-1).

In both cases, if in the same row, no adjacent two position can have the same color.

If k is the color state for the previous n position, then c = k/pow(3, n-1) is the color of the current position, so the color of the position on the right top is:  r1 = (c+1)%3 and r2 = (c+2)%3. Thus the previous states are:   k1 = [k%pow(3, n-1)]*3 + r1, and k2 = [k%pow(3, n-1)]*3 + r2.

Thus, the transition formula are:

if k is not a valid state (no adjacent positions have the same color):

    f(i, j, k) = 0

if k is a valid state:

    if j == 0

        f(i, j, k) = f(i-1, n-1, k1) + f(i-1, n-1, k2)

    if j > 0

        f(i, j, k) = f(i, j-1, k1) + f(i, j-1, k2)

So the final description of the f(i, j, k) is the number of ways to paint to the position (i, j) with the color state of the last n positions as k.

So the total number of ways of painting from (0, 0) to a specific position(i, j) is:

sum(f(i, j, k), where k is from [0, pow(3, n)).

We still have one thing to do: initialization.

For the first row, we just need to consider the current row (since there is no previous rows). For position (i, j) or (0, j), we just need to consider k < pow(3, j+1) since there are only j+1 positions in the consideration. Once k is valid, then its value can be assigned as 1 (since that once f(0, j, k) is valid, then f(0, j-1, k%pow(3, j-1)) is also valid, and, f(0, j, k) = f(0, j-1, k%pow(3, j-1)) in this case).

To make a summary, the dp idea in this question is like a "sliding-window" dp

1. for the position in the first row, we just consider the positions on the left in the same row;

2. for other row, we need to consider the previous n positions.

See the code below:


class Solution {
public:
    int colorTheGrid(int m, int n) {
        long res = 0, mod = 1e9 + 7;
        if(m<n) return colorTheGrid(n, m);
        int t = pow(3, n);
        vector<vector<vector<long>>> dp(m, vector<vector<long>>(n, vector<long>(t, 0)));
        // this is needed, o/w will TLE
        vector<vector<int>> valid(n, vector<int>(t, 0));
        for(int j=0; j<n; ++j) {
            for(int k=0; k<t; ++k) {
                valid[j][k] = isValid(k, 1, j, n);
            }
        }
        for(int i=0; i<m; ++i) {
            for(int j=0; j<n; ++j) {
                for(int k=0; k<t; ++k) {
                    if(i==0) {
                        if(k<pow(3, j+1) && isValid(k, i, j, n)) {
                            dp[i][j][k] = 1;
                            // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
                        }
                        continue;
                    }
                    if(!valid[j][k]) continue;
                    int f1 = k/(t/3), v = k%(t/3), r1 = (f1+1)%3, r2 = (f1+2)%3;
                    if(j>0) dp[i][j][k] = (dp[i][j-1][v*3+r1] + dp[i][j-1][v*3+r2])%mod;
                    else dp[i][j][k] = (dp[i-1][n-1][v*3+r1] + dp[i-1][n-1][v*3+r2])%mod;
                    // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
                }
            }
        }
        for(int i=0; i<t; ++i) {
            res += dp[m-1][n-1][i];
            res %= mod;
        }
        return res;
    }
private:
    bool isValid(int k, int i, int j, int n) {
        vector<int> vs(n, 0);
        if(i==0) vs.resize(j+1);
        int len = vs.size(), n1 = j+1, n2 = len - n1; 
        for(int id=0; id<len; ++id) {
            vs[id] = k%3;
            k /= 3;
        }
        for(int id=0; id+1<n2; ++id) {
            if(vs[id] == vs[id+1]) return false;
        }
        for(int id=n2; id+1<len; ++id) {
            if(vs[id] == vs[id+1]) return false;
        } 
        return true;
    }
};


Upper Layer

Comments

Popular posts from this blog

Segment Tree

Segment tree can be viewed as an abstract data structure which using some more space to trade for speed. For example, for a typical question with O(N^2) time complexity, the segment tree method can decrease it to O(N*log(N)).  To make it understandable, let us consider one example. Say we have an integer array of N size, and what we want is to query the maximum with a query range [idx1, idx2], where idx1 is the left indexes, and idx2 is the right indexes inclusive. If we only do this kind of query once, then we just need to scan through the array from idx1 to idx2 once, and record the maximum, done. The time complexity is O(N), which is decent enough in most cases even though it is not the optimal one (for example, with a segment tree built, the time complexity can decrease down to O(log(N))). However, how about we need to query the array N times? If we continue to use the naïve way above, then the time complexity is O(N^2), since for each query we need to scan the query range once...

Bit Manipulation - Example

  Leetcode 136 Single Number Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Constraints: 1 <= nums.length <= 3 * 10^4 -3 * 10^4 <= nums[i] <= 3 * 10^4 Each element in the array appears twice except for one element which appears only once. Analysis: If there is no space limitation, this question can be solved by counting easily. But counting requires additional space. Here we can use xor (^) operation based on some interesting observations:  A^A = 0, here A is any number A^0 = A, here A is any number Since all the number appears twice except one, then all the number appear even numbers will be cancelled out, and only the number appears one time is left, which is what we want. See the code below: class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for(auto ...

Rolling Hash

Rolling hash is one common trick used to increase efficiency of substring comparisons by compressing (or hashing) a string into a integer. After this step, we can compare two strings directly without comparing each chars. So the efficiency can be increased from O(N) to O(1). So how to implement the rolling hash? First we need to choose a base for the expansion and a modulo to mod. The basic formula is (suppose the window is n, and the rolling direction is from left to right), HashVal = (A1*p^(n-1) + A2*p^(n-2) + ... + An-1*p^1 + An*p^0)%mod where HashVal is the hash value, Ai is the ith element, p is the base, and mod is the modulo. To avoid collision as much as we can, p and modulo usually need to be large prime numbers. One corner case is that the base order in the above formula cannot be reversed. Or to be more clear, if the rolling direction is from left to right in an array, the first element should be in the highest order of the base, or times p^(n-1), and the last element times ...