463. Island Perimeter Flashcards
Can you set up variables adjacentSides and landCount?
let adjacentSides = 0, landCount = 0;
Can you loop through grid array?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
} }
In the looping, can you set up variable points to current cell?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let curr = grid[i][j;
}
}
In the looping, can you set up variable points to the top of the current cell?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let curr = grid[i][j;
let top = i > 0 ? grid[i - 1][j] : undefined;
}
}
In the looping, can you set up variable points to the left of the current cell?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let curr = grid[i][j;
let top = i > 0 ? grid[i - 1][j] : undefined;
let left = j > 0 ? grid[i][j - 1] : undefined;
}
}
When current cell value is 1, can you increase the landCount?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let curr = grid[i][j;
let top = i > 0 ? grid[i - 1][j] : undefined;
let left = j > 0 ? grid[i][j - 1] : undefined;
if (curr === 1) {
landCount++;
}
}
}
When current cell value is 1 and left cell value is 1, can you increase the adjacentSides?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let curr = grid[i][j;
let top = i > 0 ? grid[i - 1][j] : undefined;
let left = j > 0 ? grid[i][j - 1] : undefined;
if (curr === 1) {
landCount++;
if (left === 1) conSides++;
}
}
}
When current cell value is 1 and top cell value is 1, can you increase the adjacentSides?
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
let curr = grid[i][j;
let top = i > 0 ? grid[i - 1][j] : undefined;
let left = j > 0 ? grid[i][j - 1] : undefined;
if (curr === 1) {
landCount++;
if (left === 1) conSides++;
if (top === 1) conSides++;
}
}
}
Can you return the result base on landCount and adjacentSides?
return landCount * 4 - conSides * 2;