Problem Statement
Devu has a garden in his back yard.
The garden can be seen as a grid with 2 rows and N columns.
You are given a description of the garden: a
Two cells are considered adjacent if they share a side or a corner. As you may know, whenever two trees grow in adjacent cells, they hinder each other's growth. Therefore, Devu would never plant a tree into a cell that is already adjacent to a cell with a tree. (This is also true for all the trees already present in his garden.)
Given the above rule, Devu wants to plant as many additional trees as possible. Return the largest possible number of trees Devu can have in his garden at the end.
Definition
- Class:
- DevuAndPlantingTrees
- Method:
- maximumTreesDevuCanGrow
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int maximumTreesDevuCanGrow(String[] garden)
- (be sure your method is public)
Constraints
- N will be between 1 and 50, inclusive.
- garden will contain exactly 2 elements.
- Each element of garden will contain exactly N characters.
- Each character of each element of garden will be either '.' or '*'.
- No two of the already planted trees are in adjacent cells.
Examples
{"..", ".."}
Returns: 1
You can plant a single tree in either of the four available cells.
{"..", ".*"}
Returns: 1
You cannot plant any additional trees.
{"...", "..*"}
Returns: 2
The garden already contains one tree in a corner. One optimal solution is to plant one additional tree in the opposite corner.
{".....*..........", ".*.......*.*..*."}
Returns: 7
{"....*.*.*...........*........", "*..........*..*.*.*....*...*."}
Returns: 13
{".....*..*..........*............................*", "*..*.............*...*.*.*.*..*.....*.*...*...*.."}
Returns: 23
{".","."}
Returns: 1
{"..",".."}
Returns: 1
{"*..","..."}
Returns: 2
{"....","*..."}
Returns: 2
{"*....","....*"}
Returns: 3
{"....*.",".*...."}
Returns: 2
{".......","*......"}
Returns: 4
{"*.......","...*...."}
Returns: 4
{"*........","...*.*.*."}
Returns: 4
{"..*.......","......*.*."}
Returns: 5
{"....*.....*","..*........"}
Returns: 6
{".....*...*..",".......*...*"}
Returns: 6
{"..*....*.....",".....*......."}
Returns: 6
{"..*..*........","..........*..*"}
Returns: 6
{"..*...*.*.*....","*...*.......*.*"}
Returns: 8
{"..............*.",".....*..*..*...."}
Returns: 6
{"...*.*.*.........","*.........*..*.*."}
Returns: 7
{"*.*....*.....*.*..","...........*......"}
Returns: 9
{"....*..*...........",".*.......*..*.*...."}
Returns: 8
{"..*.*.*.*...*.......","...............*.*.."}
Returns: 10
{".....*...........*.*.","..*....*.*...*.*....."}
Returns: 10
{"........*.......*....*",".*.*..*....*.........."}
Returns: 9
{"...*.*..............*..","........*...*.*.*......"}
Returns: 11
{".....*..................","*.*....*....*.*..*...*.*"}
Returns: 11
{"...........*......*......","..*...........*.....*..*."}
Returns: 11
{"......*.....*.*..*.*......",".*......*.............*.*."}
Returns: 11
{".*.*......*..*......*.....*","......*.*........*....*.*.."}
Returns: 12
{"..*...*........*...*..*.*.*.","........*..*.*.............."}
Returns: 13
{".*............*...*..*.....*.","...*.*.*....*..........*....."}
Returns: 13
{"..........*.*.......*......*..","*.*.*.........*...*.....*....."}
Returns: 15
{"*.*........*..*..........*....*","....*....*......*.*..*........."}
Returns: 14
{"...*........*..*...........*....","*.....*..........*...*.*.*...*.*"}
Returns: 15
{".*...*.*......*......*...........",".................*.*...*.....*..."}
Returns: 15
{".......*.*.*.*.......*...*.*...*..","*.*..............*................"}
Returns: 17
{".*.............*...*..........*..*.","....*..*..*..*...*......*..*......."}
Returns: 13
{"...*....*..*........*...*.....*....*",".*...*.......*..............*......."}
Returns: 16
{"..*.....*.*..............*...*.....*.","....*.*.....*.....*....*........*...."}
Returns: 17
{"*...*....*.*.....*.*....*.....*....*.*","..*...........*......*..........*....."}
Returns: 17
{".*...*.....*.*........*........*.....*.","...*.....*.....*.*..*......*.*...*.*..."}
Returns: 18
{"...*..............*..*.............*....","......*.*.*..*.................*......*."}
Returns: 17
{"..*.......*...*.....*.*.....*.*..........","......*.........*..................*.*.*."}
Returns: 20
{".........*..*...*........*......*.........",".*...*.*......*...*....*....*.*..........*"}
Returns: 19
{"...*...*..*...*.*.*.....................*.*",".*...*..............*.*..........*..*......"}
Returns: 20
{"*...........*..............*..*..........*..","...*..*.*.*....*...*..*..*.............*...*"}
Returns: 19
{".........................*.*.........*.......","*.*.*..*.....*.*...*...*.......*.*.*.....*..*"}
Returns: 22
{"*...*...*...*.*.............*.*...*.*..*.*..*.","..*...*...*.....*.*...*......................."}
Returns: 22
{"....*...*...*...*..*............*.....*..*..*..","*.*...................*..*.*..*...*.*.........."}
Returns: 21
{"...*..*.*.*...*......*..*.....*..*.......*.*....","*...........*...*.*........*........*.........*."}
Returns: 19
{"........*.*.*..*....*.*.*.........*..*....*......","*..*.............*..........*.*.........*......*."}
Returns: 21
{".*..*.....*................*..................*...",".............*..*..*.*..*.....*...*...*.*...*...*."}
Returns: 21
{".", "." }
Returns: 1
{"*", "." }
Returns: 1
{"....*.*.*...........*........", "*..........*..*.*.*....*...*." }
Returns: 13
{"...", "*.." }
Returns: 2
{".....*..*..........*............................*", "*..*.............*...*.*.*.*..*.....*.*...*...*.." }
Returns: 23
{".*", ".." }
Returns: 1
{".", "*" }
Returns: 1
{"...", "..*" }
Returns: 2
{"...", "..." }
Returns: 2
{"*..", "..." }
Returns: 2
{"...*", "...." }
Returns: 2