I am using Scala to solve the problem documented here: https://leetcode.com/problems/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 all surrounded by water.
Example 1:
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"] ] Output: 1
Example 2:
Input: grid = [
["1","1","0","0","0"], ["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"] ] Output: 3
I thought it was a simple application of Breadth (or depth) First Search. However, my program produces a Memory Limit Exceeded error at Leetcode. I also ran the program on my laptop. My Mac reports it uses more than 3 GB memory, even if I'd commented out 3 lines of the test case. What consumes so much memory in my code?
// Memory OVERFLOW !!? Why ??
class Leet0200 {
def numIslands(grid: Array[Array[Char]]): Int = {
var (y, x) = getPosition(grid)
var c = 0
val dirs = Array(
Array(1, 0),
Array(-1, 0),
Array(0, 1),
Array(0, -1)
)
while ((y, x) != (-1, -1)) {
c += 1
val queue = scala.collection.mutable.Queue[(Int, Int)]()
queue.enqueue((y,x))
while (queue.nonEmpty) {
val me = queue.dequeue()
val (me_y, me_x) = me
grid(me_y)(me_x) = 'c'
for (d <- dirs) {
val (ny, nx) = (me_y + d(0), me_x+d(1))
if (ny >= 0 && ny < grid.length
&& nx >= 0 && nx < grid(0).length
&& grid(ny)(nx) == '1') {
queue.enqueue((ny, nx))
}
}
}
val newPos = getPosition(grid)
y = newPos._1
x = newPos._2
}
c
}
def getPosition(grid: Array[Array[Char]]): (Int, Int) = {
for (y <- grid.indices) {
for (x <- grid(0).indices) {
if (grid(y)(x) == '1') return (y, x)
}
}
(-1, -1)
}
}
object Leet0200 {
def main(args: Array[String]): Unit = {
val leet = new Leet0200()
println(
leet.numIslands(Array(
Array('1','1','1','1','1','0','1','1','1','1','1','1','1','1','1','0','1','0','1','1'),
Array('0','1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','0'),
Array('1','0','1','1','1','0','0','1','1','0','1','1','1','1','1','1','1','1','1','1'),
Array('1','1','1','1','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
Array('1','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
Array('1','0','1','1','1','1','1','1','0','1','1','1','0','1','1','1','0','1','1','1'),
Array('0','1','1','1','1','1','1','1','1','1','1','1','0','1','1','0','1','1','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','0','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','1','1','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
Array('0','1','1','1','1','1','1','1','0','1','1','1','1','1','1','1','1','1','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
Array('1','1','1','1','1','0','1','1','1','1','1','1','1','0','1','1','1','1','1','1'),
Array('1','0','1','1','1','1','1','0','1','1','1','0','1','1','1','1','0','1','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','1','0'),
//Array('1','1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','0','0'),
//Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
//Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1')
)))
}
}