forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTilingProblem.java
More file actions
56 lines (38 loc) · 1.67 KB
/
TilingProblem.java
File metadata and controls
56 lines (38 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Probelm Statement:
Given: (2 x n) board and tiles of size (2 x 1)
Task: Count the number of ways to tile the given board using the 2 x 1 tiles.
Conditions: A tile can either be placed horizontally as (1 x 2) tile or vertically as (2 x 1) tile.
Solution:
If we place 1 tile vertically, it occupies (2 x 1) area
OR
We can place 2 tiles horizontally, one above another to cover (2 x 2) area
There is no other way
so we form our recursion so as to count the number of ways to to fill the board horizontally with
either 1's or 2's
Recurrence Equation:
count(n) = count(n-1) + count(n-2)
Base conditions:
if n == 1, return 1
if n == 2, return 2
Bottom Up DP Approach:
We can calculate the values while looping over 1 to N considering required lookbacks
*/
class TilingProblem {
// start from bottom and calculate for each step using previous result
static int solve(int n) {
int dp[] = new int[n+1];
dp[1]=1; dp[2]=2; // Preconditions and initializations
for(int i=3;i<=n;i++) {
dp[i]=dp[i-1]+dp[i-2]; //using the recurence equation to transform to iterative solution
}
return dp[n]; // finally return the solution for the query value N
}
// Main function of the code
public static void main(String args[]) {
int n, total_number_of_ways;
n = 12; // The board is of size (2 x 12)
total_number_of_ways = solve(n); // Calling the solve function with parameter value N
System.out.println("The Total Number of ways to Tile the board are: "+total_number_of_ways);
}
}