Count number of ways to fill a “n x 4” grid using “1 x 4” tiles
Last Updated :
15 Nov, 2023
Given a number n, count number of ways to fill a n x 4 grid using 1 x 4 tiles.
Examples:
Input : n = 1
Output : 1
Input : n = 2
Output : 1
We can only place both tiles horizontally
Input : n = 3
Output : 1
We can only place all tiles horizontally.
Input : n = 4
Output : 2
The two ways are :
1) Place all tiles horizontally
2) Place all tiles vertically.
Input : n = 5
Output : 3
We can fill a 5 x 4 grid in following ways :
1) Place all 5 tiles horizontally
2) Place first 4 vertically and 1 horizontally.
3) Place first 1 horizontally and 4 vertically.
This problem is mainly an extension of this tiling problem
Let “count(n)” be the count of ways to place tiles on a “n x 4” grid, following two cases arise when we place the first tile.
- Place the first tile horizontally : If we place first tile horizontally, the problem reduces to “count(n-1)”
- Place the first tile vertically : If we place first tile vertically, then we must place 3 more tiles vertically. So the problem reduces to “count(n-4)”
Therefore, count(n) can be written as below.
count(n) = 1 if n = 1 or n = 2 or n = 3
count(n) = 2 if n = 4
count(n) = count(n-1) + count(n-4)
This recurrence is similar to Fibonacci Numbers and can be solved using Dynamic programming.
C++
#include<iostream>
using namespace std;
int count( int n)
{
int dp[n+1];
dp[0] = 0;
for ( int i=1; i<=n; i++)
{
if (i >= 1 && i <= 3)
dp[i] = 1;
else if (i==4)
dp[i] = 2 ;
else
dp[i] = dp[i-1] + dp[i-4];
}
return dp[n];
}
int main()
{
int n = 5;
cout << "Count of ways is " << count(n);
return 0;
}
|
Java
import java.io.*;
class Grid
{
static int count( int n)
{
int [] dp = new int [n+ 1 ];
dp[ 0 ] = 0 ;
for ( int i= 1 ;i<=n;i++)
{
if (i >= 1 && i <= 3 )
dp[i] = 1 ;
else if (i== 4 )
dp[i] = 2 ;
else
{
dp[i] = dp[i- 1 ] + dp[i- 4 ];
}
}
return dp[n];
}
public static void main (String[] args)
{
int n = 5 ;
System.out.println( "Count of ways is: " + count(n));
}
}
|
Python3
def count(n):
dp = [ 0 for _ in range (n + 1 )]
for i in range ( 1 ,n + 1 ):
if i < = 3 :
dp[i] = 1
elif i = = 4 :
dp[i] = 2
else :
dp[i] = dp[i - 1 ] + dp[i - 4 ]
return dp[n]
n = 5
print ( "Count of ways is" ),
print (count(n))
|
C#
using System;
class GFG
{
static int count( int n)
{
int [] dp = new int [n + 1];
dp[0] = 0;
for ( int i = 1; i <= n; i++)
{
if (i >= 1 && i <= 3)
dp[i] = 1;
else if (i == 4)
dp[i] = 2 ;
else
{
dp[i] = dp[i - 1] +
dp[i - 4];
}
}
return dp[n];
}
public static void Main ()
{
int n = 5;
Console.WriteLine( "Count of ways is: "
+ count(n));
}
}
|
Javascript
<script>
function count(n)
{
let dp = [];
dp[0] = 0;
for (let i = 1; i <= n; i++)
{
if (i >= 1 && i <= 3)
dp[i] = 1;
else if (i == 4)
dp[i] = 2 ;
else
{
dp[i] = dp[i - 1] + dp[i - 4];
}
}
return dp[n];
}
let n = 5;
document.write( "Count of ways is: " + count(n));
</script>
|
PHP
<?php
function countt( $n )
{
$dp [ $n + 1] = 0;
$dp [0] = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $i >= 1 && $i <= 3)
$dp [ $i ] = 1;
else if ( $i == 4)
$dp [ $i ] = 2 ;
else
$dp [ $i ] = $dp [ $i - 1] + $dp [ $i - 4];
}
return $dp [ $n ];
}
$n = 5;
echo "Count of ways is " , countt( $n );
?>
|
Output :
Count of ways is 3
Time Complexity : O(n)
Auxiliary Space : O(n)
Efficient approach : Space Optimization
In previous approach we the current value dp[i] is only depend upon the previous 2 values i.e. dp[i-1] and dp[i-4]. So to optimize the space we can keep track of previous 4 values and current values which will reduce the space complexity from O(N) to O(1).
Implementation Steps:
- Handle the base cases:
- If n is less than or equal to 0, return 0.
- If n is less than or equal to 3, return 1.
- Initialize variables dp1, dp2, dp3, and dp4 with values 1, 1, 1, and 2 respectively.
- Initialize dp to 0.
- Iterate from 5 to n:
- Calculate dp as the sum of dp4 and dp1.
- Update the variables: shift dp1 to dp2, dp2 to dp3, dp3 to dp4, and dp4 to dp.
- Return dp as the count of ways.
Implementation:
C++
#include<iostream>
using namespace std;
int count( int n)
{
if (n <= 0)
return 0;
if (n <= 3)
return 1;
int dp1 = 1;
int dp2 = 1;
int dp3 = 1;
int dp4 = 2;
int dp = 0;
for ( int i = 5; i <= n; i++)
{
dp = dp4 + dp1;
dp1 = dp2;
dp2 = dp3;
dp3 = dp4;
dp4 = dp;
}
return dp;
}
int main()
{
int n = 5;
cout << "Count of ways is " << count(n) << endl;
return 0;
}
|
Java
import java.io.*;
class Gfg {
static int count( int n) {
if (n <= 0 )
return 0 ;
if (n <= 3 )
return 1 ;
int dp1 = 1 ;
int dp2 = 1 ;
int dp3 = 1 ;
int dp4 = 2 ;
int dp = 0 ;
for ( int i = 5 ; i <= n; i++) {
dp = dp4 + dp1;
dp1 = dp2;
dp2 = dp3;
dp3 = dp4;
dp4 = dp;
}
return dp;
}
public static void main(String[] args) {
int n = 5 ;
System.out.println( "Count of ways is " + count(n));
}
}
|
Python3
def count(n):
if n < = 0 :
return 0
if n < = 3 :
return 1
dp1 = 1
dp2 = 1
dp3 = 1
dp4 = 2
dp = 0
for i in range ( 5 , n + 1 ):
dp = dp4 + dp1
dp1, dp2, dp3, dp4 = dp2, dp3, dp4, dp
return dp
if __name__ = = "__main__" :
n = 5
print ( "Count of ways is" , count(n))
|
C#
using System;
class WaysToPlaceTiles
{
static int Count( int n)
{
if (n <= 0)
return 0;
if (n <= 3)
return 1;
int dp1 = 1;
int dp2 = 1;
int dp3 = 1;
int dp4 = 2;
int dp = 0;
for ( int i = 5; i <= n; i++)
{
dp = dp4 + dp1;
dp1 = dp2;
dp2 = dp3;
dp3 = dp4;
dp4 = dp;
}
return dp;
}
static void Main( string [] args)
{
int n = 5;
Console.WriteLine( "Count of ways is " + Count(n));
}
}
|
Javascript
function count(n) {
if (n <= 0) {
return 0;
}
if (n <= 3) {
return 1;
}
let dp1 = 1;
let dp2 = 1;
let dp3 = 1;
let dp4 = 2;
let dp = 0;
for (let i = 5; i <= n; i++) {
dp = dp4 + dp1;
dp1 = dp2;
dp2 = dp3;
dp3 = dp4;
dp4 = dp;
}
return dp;
}
const n = 5;
console.log( "Count of ways is " + count(n));
|
Output :
Count of ways is 3
Time Complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Rajat Jha.
Share your thoughts in the comments
Please Login to comment...