Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]] Output: 4
Example 3:
Input: grid = [[1,0]] Output: 4
Constraints:
row == grid.lengthcol == grid[i].length1 <= row, col <= 100grid[i][j] is 0 or 1.grid.Problem summary: You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
[[1]]
[[1,0]]
max-area-of-island)flood-fill)coloring-a-border)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #463: Island Perimeter
class Solution {
public int islandPerimeter(int[][] grid) {
int ans = 0;
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
ans += 4;
if (i < m - 1 && grid[i + 1][j] == 1) {
ans -= 2;
}
if (j < n - 1 && grid[i][j + 1] == 1) {
ans -= 2;
}
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #463: Island Perimeter
func islandPerimeter(grid [][]int) int {
m, n := len(grid), len(grid[0])
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
ans += 4
if i < m-1 && grid[i+1][j] == 1 {
ans -= 2
}
if j < n-1 && grid[i][j+1] == 1 {
ans -= 2
}
}
}
}
return ans
}
# Accepted solution for LeetCode #463: Island Perimeter
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
ans += 4
if i < m - 1 and grid[i + 1][j] == 1:
ans -= 2
if j < n - 1 and grid[i][j + 1] == 1:
ans -= 2
return ans
// Accepted solution for LeetCode #463: Island Perimeter
struct Solution;
impl Solution {
fn island_perimeter(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let m = grid[0].len();
let mut sum = 0;
for i in 0..n {
for j in 0..m {
if grid[i][j] == 1 {
if i > 0 && grid[i - 1][j] == 0 || i == 0 {
sum += 1;
}
if i < n - 1 && grid[i + 1][j] == 0 || i == n - 1 {
sum += 1;
}
if j > 0 && grid[i][j - 1] == 0 || j == 0 {
sum += 1;
}
if j < m - 1 && grid[i][j + 1] == 0 || j == m - 1 {
sum += 1;
}
}
}
}
sum
}
}
#[test]
fn test() {
let grid: Vec<Vec<i32>> = vec![
vec![0, 1, 0, 0],
vec![1, 1, 1, 0],
vec![0, 1, 0, 0],
vec![1, 1, 0, 0],
];
assert_eq!(Solution::island_perimeter(grid), 16);
}
// Accepted solution for LeetCode #463: Island Perimeter
function islandPerimeter(grid: number[][]): number {
let m = grid.length,
n = grid[0].length;
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
let top = 0,
left = 0;
if (i > 0) {
top = grid[i - 1][j];
}
if (j > 0) {
left = grid[i][j - 1];
}
let cur = grid[i][j];
if (cur != top) ++ans;
if (cur != left) ++ans;
}
}
// 最后一行, 最后一列
for (let i = 0; i < m; ++i) {
if (grid[i][n - 1] == 1) ++ans;
}
for (let j = 0; j < n; ++j) {
if (grid[m - 1][j] == 1) ++ans;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.