Rotting Oranges

In this tutorial, i am going to discuss a very interesting problem rotting oranges (minimum time required to rot all fresh oranges).

Given an m*n grid where each cell in the matrix can have values 0, 1 or 2.

0 represents an empty cell
1 represents cells have fresh oranges
2 represents cells have rotten oranges

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

Number of Islands

In this tutorial, I am going to discuss a very famous interview problem. How to count the number of islands.

Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.

You may assume all four edges of the grid are surrounded by water.