You can use a board that is 2 x n size. The tiles are of size 2 x 1. Count the number of ways to tile the board. Tiles can be placed vertically 2 x 1 or horizontally as 1 x 2.
Input: int board = 4; Output: 5 Explanation: For a 2 x 4 board there are 5 ways to tile: 1 way is with all tiles vertical, 1 way is with all horizontal, and 3 ways having the 4 tiles displayed 2 vertical and 2 horizontal. |
Solution:
1. static int getNoOfWaysToTile(int board) { 2. if (board <= 2) { 3. return board; 4. } 5. return getNoOfWaysToTile(board - 1) + getNoOfWaysToTile(board - 2); 6. } 7.
Comments