Skip to content

Commit

Permalink
Runtime: 215 ms, faster than 80.55% of JavaScript online submissions …
Browse files Browse the repository at this point in the history
…for Count Sub Islands.

Memory Usage: 62 MB, less than 68.52% of JavaScript online submissions for Count Sub Islands.
  • Loading branch information
masiiie committed Jul 16, 2022
1 parent 4f685b1 commit 2bf112d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions count-sub-islands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
*/
var countSubIslands = function(grid1, grid2) {
let answer = 0;

var notCovered = function(r, c){
// No hay array booleano porque los visitados se marcan en grid2 con un 2
var area = function(r, c){
if(r < 0 || c < 0 || r == grid1.length || c == grid1[0].length || grid2[r][c] != 1) return 0;

grid2[r][c] = 2;
return (grid1[r][c] == grid2[r][c] ? 0 : 1) + notCovered(r, c-1) + notCovered(r-1, c) + notCovered(r, c+1) + notCovered(r+1, c);
return (grid1[r][c] === 1 ? 0 : 1) + area(r, c-1) + area(r-1, c) + area(r, c+1) + area(r+1, c);
}

for (let i = 0; i < grid1.length; i++) {
for (let j = 0; j < grid1[0].length; j++) {
if(notCovered(i, j) == 0) answer++;
if(grid2[i][j] === 1 && area(i, j) === 0) answer++;
}
}
return answer;
Expand Down

0 comments on commit 2bf112d

Please sign in to comment.