Back to course home
0% completed
Vote For New Content
Any idea why Math.max(islandCounter) is producing a NaN null value ?
venkat narsim
Aug 4, 2023
class Solution { maxAreaOfIsland(matrix) { let biggestIslandArea = 0; let islandCounter = []; let row = matrix.length; let col = matrix[0].length; let visitedMatrix = Array(row).fill(false).map(() => Array(col).fill(false)); for(let i=0; i<row; i++) { for(let j=0; j<col; j++) { if(matrix[i][j] === 1 && visitedMatrix[i][j] === false) { biggestIslandArea = visitedMatrixFiller(matrix, visitedMatrix, biggestIslandArea, i,j); islandCounter.push(biggestIslandArea); biggestIslandArea = 0; } } } console.log(islandCounter); --> This produces [4,1,5] return Math.max(islandCounter); --> This produces null } } function visitedMatrixFiller(matrix, visitedMatrix, biggestIslandArea, i,j) { const neighbors = [[i,j]]; while(neighbors.length > 0) { const cell = neighbors.shift(); let row = cell[0]; let col = cell[1]; if(row < 0 || row >= matrix.length || col < 0 || matrix[0].length <= col) continue; if(visitedMatrix[row][col] === true || matrix[row][col] === 0) continue; visitedMatrix[row][col] = true; biggestIslandArea++; neighbors.push([row-1, col]); neighbors.push([row+1, col]); neighbors.push([row, col-1]); neighbors.push([row, col+1]); } return biggestIslandArea }
0
0
Comments
Comments
On this page