184 contest

Number of schemes to color N x 3 grid

 

You have an nx 3 grid grid. You need to color each grid with one of red, yellow, and green, and make sure that the adjacent grids are different in color (that is, grids with the same horizontal or vertical sides Different colors).

Gives you the number of rows in the grid graph n.

Please return the number of schemes painted to grid. Since the answer may be very large, please return the result of the answer pair 10 ^ 9 + 7.

 

Example 1:

Input: n = 1
Output: 12
Explanation: There are 12 possible methods in total:

 

 

Example 2:

Input: n = 2
Output: 54
Example 3:

Input: n = 3
Output: 246
Example 4:

Input: n = 7
Output: 106494
Example 5:

Input: n = 5000
Output: 30228214
 

prompt:

n == grid.length
grid[i].length == 3
1 <= n <= 5000

 

/ * * 
 * @param {number} n 
 * @return {number} 
 * / 
// The result of finding the lower layer of the rule = the number of ABA classes + the number of ABC classes = (3m + 2n) + (2m + 2n) 
var numOfWays = function (n) {
     if (n === 0 ) {
         return 0 
    } else  if (n === 1 ) {
         return 12 
    } 
    let temp = 1000000007 ; 
    let repeat = 6 ; 
    let unrepeat = 6 ;
     for (let i = 2; i <= n; i ++ ) { 
        let newrep = repeat * 3% temp + unrepeat * 2% temp;
        let newunrep = repeat *2 % temp + unrepeat *2 % temp;
        repeat = newrep;
        unrepeat = newunrep;
    }
    return (repeat + unrepeat)%temp
};

 

/ * * 
 * @param {number} n 
 * @return {number} 
 * / 
// I was thinking at the time, is it a combination, but the second column of the second row is actually not 1. Is it possible 1, is possible 2, the situation is missing. 
var numOfWays = function (n) { 
 let arr   = new Array (n);
     for (let i = 0; i <n; i ++ ) { 
        arr [i] = new Array (3 ) 
    } 
    let row = arr.length; 
    let clo = 3 ;
     for (let i = 0; i <row; i ++ ) {
         for (let j = 0; j <clo; j ++ ) {
             if (i == 0 && j === 0 ) {
                arr[i][j] = 3; 
            } else if(i==0 && j>0){
                arr[i][j] = 2;        
            } else if(i>0 && j===0){
                arr[i][j] = 2;        
            } else {
                arr[i][j] = 1;
            }
        }
    }
    
    let res = 1;
    for(let i=0; i<row; i++){
        for(let j=0; j<clo; j++){
            res*=arr[i][j];
            res%=Math.pow(10, 9)+7
        }
    }
    for(let i=0; i<row; i++){
        for(let j=0; j<clo; j++){
            if(i==0 && j === 0){
                arr[i][j] = 3; 
            } else if(i==0 && j>0){
                arr[i][j] = 2;        
            } else if(i>0 && j===0){
                arr[i][j] = 2;        
            } else {
                arr[i][j] = 1;
            }
        }
    }
    // arr[0][0] = 3;
//     for(let i=0; i<arr.length; i++){
//         for(let j=0; j<arr[0].length; j++){
//             if((arr[i-1] && arr[i-1][j] === undefined && arr[i] && arr[i][j-1] !== undefined) || (arr[i-1] && arr[i-1][j] !== undefined && arr[i] && arr[i][j-1] === undefined)){
//                 arr[i][j] = 2
//             } else if(arr[i-1] && arr[i-1][j] !== undefined && arr[i] && arr[i][j-1] !== undefined){
//                   arr[i][j] = 1    
//             } else if(arr[i-1] && arr[i-1][j] == undefined && arr[i] && arr[i][j-1] == undefined){
//                 arr[i][j] = 3  
//             }
            
//         }
//     }
    return res;
};

 

Source: LeetCode
Link: https://leetcode-cn.com/problems/number-of-ways-to-paint-nx-3-grid
Copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Guess you like

Origin www.cnblogs.com/zhangzs000/p/12728857.html