# Shortest Bridge - Google Top Interview Questions

### Problem Statement :

```Given a two-dimensional list of integers matrix containing 0s and 1s, 0 represents water and 1 represents land.

An island is a group of connecting 1s in 4 directions that are either surrounded by 0s or by the edges.
Find the shortest bridge that connects two islands.

It is guaranteed that there are two and only two islands.

Constraints

n, m ≤ 250 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [

[0, 1],

[1, 0]

]

Output

1

Explanation

Either of the two water elements can be used as the bridge.

Example 2

Input

matrix = [

[1, 0, 0],

[0, 0, 0],

[0, 0, 1]

]

Output

3

Explanation

There's six shortest bridges such as [(0, 1), (0, 2), (1, 2)] or [(1, 0), (2, 0), (2, 1)]```

### Solution :

```                        ```Solution in C++ :

void dfs(vector<vector<int>> &matrix, int x, int y, queue<pair<int, int>> &q) {
// Invalid position or cell-state
if (x < 0 || y < 0 || x >= matrix.size() || y >= matrix.size() || matrix[x][y] == 2 ||
matrix[x][y] < 0) {
return;
}

// If the cell is a water cell adjacent to the island
// mark it and place it into the bfs queue and return
if (matrix[x][y] == 0) {
matrix[x][y] = -1;
q.emplace(make_pair(x, y));
return;
}

// Mark island cell with different color
if (matrix[x][y] == 1) {
matrix[x][y] = 2;
}

// run dfs to mark neighbors
vector<vector<int>> dirs{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

for (auto &dir : dirs) {
int x1 = x + dir;
int y1 = y + dir;

dfs(matrix, x1, y1, q);
}
}

int solve(vector<vector<int>> &matrix) {
int m = matrix.size(), n = m == 0 ? 0 : matrix.size();

// Find the first land cell (can be any of the two islands)
// and run dfs to mark the island with a different color
bool landFound = false;
queue<pair<int, int>> q;
for (int x = 0; x < m && !landFound; ++x) {
for (int y = 0; y < n && !landFound; ++y) {
if (matrix[x][y] == 1) {
landFound = true;
dfs(matrix, x, y, q);
}
}
}

// Means that there's no island at all
// so return error
if (q.empty()) {
return -1;
}

// Run bfs from the edges of the marked island
vector<vector<int>> dirs{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

while (!q.empty()) {
pair<int, int> curr = q.front();
q.pop();

for (auto &dir : dirs) {
int x = curr.first + dir;
int y = curr.second + dir;

if (x >= 0 && y >= 0 && x < m && y < n) {
// If we have found the second island
// return the distance
// distance is -ve of the current cell
// since we've marked water cells with
// increasing negative values
if (matrix[x][y] == 1) {
return 0 - matrix[curr.first][curr.second];
}
// If it is a water cell, update its distance
// and then add it to the queue
else if (matrix[x][y] == 0) {
matrix[x][y] = matrix[curr.first][curr.second] - 1;
q.emplace(make_pair(x, y));
}
}
}
}

// We didn't find the second island so return error
return -1;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
private static final int[] DIRS = {-1, 0, 1, 0, -1};
private static final int NEWCOLOR = 2;

public int solve(int[][] grid) {
final int R = grid.length, C = grid.length;

Queue<int[]> q = new ArrayDeque<>();
findIsland(grid, R, C, q);

for (int step = 0; q.isEmpty() == false; step++) {
for (int b = q.size(); b != 0; b--) {
int[] now = q.poll();
for (int i = 0; i != 4; i++) {
final int nr = now + DIRS[i], nc = now + DIRS[i + 1];
if (nr == -1 || nr == R || nc == -1 || nc == C || grid[nr][nc] == NEWCOLOR)
continue;
if (grid[nr][nc] == 1)
return step;
grid[nr][nc] = NEWCOLOR;
q.offer(new int[] {nr, nc});
}
}
}

return -1;
}

private void findIsland(int[][] grid, final int R, final int C, Queue<int[]> q) {
for (int r = 0; r != R; r++)
for (int c = 0; c != C; c++)
if (grid[r][c] == 1) {
drown(grid, R, C, r, c, NEWCOLOR, q);
return;
}
}

private void drown(int[][] grid, int R, int C, int r, int c, int color, Queue<int[]> q) {
if (r == R || c == C || r == -1 || c == -1 || grid[r][c] != 1)
return;
grid[r][c] = color;
q.offer(new int[] {r, c});
for (int i = 0; i != 4; i++) drown(grid, R, C, r + DIRS[i], c + DIRS[i + 1], color, q);
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, matrix):
cnt = 0

rows = len(matrix)
columns = len(matrix)
visited = {}

for i in range(2):
flag = True

while flag:
column = cnt % columns
row = cnt // columns

if (row, column) not in visited:
visited[(row, column)] = 0

if matrix[row][column] == 1:
flag = False
matches = {(row, column): 0}
self.find_touching(matrix, visited, matches, row, column, rows, columns)

cnt += 1

if i == 0:
island_one = self.find_outline(matches)
print(island_one)
else:
island_two = self.find_outline(matches)
print(island_two)

best = -1

for pos1 in island_one:
for pos2 in island_two:
needed = abs(pos1 - pos2) + abs(pos1 - pos2) - 1
if best == -1 or needed < best:
best = needed

return best

def find_touching(self, matrix, visited, matches, row, column, rows, columns):
moves = [(-1, 0), (0, -1), (0, 1), (1, 0)]

for move in moves:
new_row = row + move
new_column = column + move

if (new_row, new_column) not in visited:
if new_row >= 0 and new_row < rows:
if new_column >= 0 and new_column < columns:
visited[(new_row, new_column)] = 0
if matrix[new_row][new_column] == 1:
matches[(new_row, new_column)] = 0
self.find_touching(
matrix, visited, matches, new_row, new_column, rows, columns
)

return matrix

def find_outline(self, matches):
keys = list(matches.keys())
for item in keys:
flag = True
for direction in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if (item + direction, item + direction) not in matches:
flag = False
break
if flag:
matches[item] = -1

return [x for x in matches if matches[x] == 0]```
```

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the