Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Having problems solving Leetcode Question Max Area of Islands

Below is my code to find the max area of islands but for some reason it kept failing this test case: [[1],[1]]

Im not entirely sure whats wrong and Ive been thinking to no avail of a possible solution using this method. It was thinking a possible reason why this code is not working properly is because of the synchronous nature of JS when the dfs() is called in the for loop at the bottom.

var maxAreaOfIsland = function(grid) {
        let maxArea = 0
    let currArea = 0

    let dirs = [
        [0,1],
        [1,0],
        [-1,0],
        [0,-1]
    ]

    function dfs(r,c){
        if(grid[r][c] === 0) return
        
        currArea++
        grid[r][c] = 0
        for(let [row,col] of dirs){
            let nr = r+row
            let nc = c+col
            if(nr<0 || nc< 0|| nr >= grid.length|| nc >= grid[0].length){
                return
            }
            dfs(nr,nc)
        }
    }

    for(let i = 0; i < grid.length; i++){
        for(let j = 0; j< grid[0].length; j++){
            if(grid[i][j] === 1){
                
                currArea = 0
                dfs(i,j)
                if(currArea > maxArea){
                    maxArea = currArea
                }
            }
        }
    }

    return maxArea
};

console.log(maxAreaOfIsland( [[1],[1]] ))

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this: I guess you need to check if the cell is valid in grid before going forward.

var maxAreaOfIsland = function(grid) {
        let maxArea = 0
    let currArea = 0

    let dirs = [
        [0,1],
        [1,0],
        [-1,0],
        [0,-1]
    ]

    function dfs(r,c){
        if(r<0 || c< 0|| r >= grid.length|| c >= grid[0].length)
                return
        if(grid[r][c] === 0) return
        
        currArea++
        grid[r][c] = 0
        for(let [row,col] of dirs){
            let nr = r+row
            let nc = c+col
            dfs(nr,nc)
        }
    }

    for(let i = 0; i < grid.length; i++){
        for(let j = 0; j< grid[0].length; j++){
            if(grid[i][j] === 1){
                
                currArea = 0
                dfs(i,j)
                if(currArea > maxArea){
                    maxArea = currArea
                }
            }
        }
    }

    return maxArea
};

console.log(maxAreaOfIsland( [[1],[1]] ))

I recomend using C++ as the same code will take 8 time less compared to JS

about 4 years ago · Juan Pablo Isaza Relatório

0

It looks as if the dfs function is meant search around a non-zero location, incrementing currArea as it finds more non-zeros, and taking care not to run off the edge of the matrix. The return in that boundary check, however, gives up the search when it should just continue. One change, marked with a comment...

var maxAreaOfIsland = function(grid) {
        let maxArea = 0
    let currArea = 0

    let dirs = [
        [0,1],
        [1,0],
        [-1,0],
        [0,-1]
    ]

    function dfs(r,c){
        if(grid[r][c] === 0) return
        
        currArea++
        grid[r][c] = 0
        for(let [row,col] of dirs){
            let nr = r+row
            let nc = c+col
            if(nr<0 || nc< 0|| nr >= grid.length|| nc >= grid[0].length){
                continue;  // <-- notice, we keep searching
            }
            dfs(nr,nc)
        }
    }

    for(let i = 0; i < grid.length; i++){
        for(let j = 0; j< grid[0].length; j++){
            if(grid[i][j] === 1){
                
                currArea = 0
                dfs(i,j)
                if(currArea > maxArea){
                    maxArea = currArea
                }
            }
        }
    }

    return maxArea
};

console.log(maxAreaOfIsland( [[1],[1]] ))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda