Graphs, BFS, and grid traversal
rows x cols grid where grid[i][j] is either 0 (water) or 1 (land). The grid contains exactly one island (one or more connected land cells). The island doesn't have any "lakes" (water inside that isn't connected to the water around the island). Cells are connected horizontally/vertically (not diagonally). Determine the perimeter of the island.
Each land cell contributes 4 edges. But when two land cells are adjacent, the shared edge is interior and doesn't count toward the perimeter.
So the perimeter = total edges from land cells minus the shared interior edges.
Use BFS starting from any land cell. Add the cell to a queue. While the queue is not empty, pop a cell and check all 4 neighbours. Each neighbour that is water or out of bounds adds 1 to the perimeter. Enqueue unvisited land neighbours.
⚙ Knowledge Hub: Using Graphs1) in the grid-1)n nodes labelled from 1 to n. A star graph is a graph where there is one centre node and exactly n - 1 edges that connect the centre node with every other node. You are given a 2D integer array edges where each edges[i] = [u, v] indicates that there is an edge between nodes u and v. Return the centre of the given star graph.
A star graph has one centre connected to all other nodes. The centre appears in every edge. Every other node appears in exactly one edge.
In graph terminology, the centre has degree n-1 (the highest degree).
Build an adjacency list from the edge list (a fundamental graph operation). Then run a BFS from any node using a queue. The centre node is the one whose adjacency list length equals the number of edges (i.e. it connects to every other node).
⚙ Knowledge Hub: Using Graphs[u, v], add v to adj[u] and u to adj[v]n - 1 (i.e. len(edges)) is the centreedges[0] and edges[1]. Find the common nodem x n integer matrix grid, and three integers row, col, and color. Each value in the grid represents the colour of the grid square at that location. Two squares are adjacent if they are next to each other in any of the 4 directions. Two squares belong to the same connected component if they have the same colour and they are adjacent. The border of a connected component is all squares in the component that are either adjacent to a square not in the component, or on the boundary of the grid. Colour the border of the connected component that contains grid[row][col] with color. Return the final grid.
You need to find the connected component that includes grid[row][col]. Then identify which cells in that component are on the border.
A cell is a border cell if it's on the edge of the grid OR at least one of its 4 neighbours has a different colour (i.e. isn't part of the same component).
Only re-colour the border cells; leave interior cells unchanged.
Use BFS starting from (row, col) to find all cells in the connected component. Add the starting cell to a queue and process neighbours level by level. As you visit each cell, check if it's a border cell.
(row, col)(row, col) using a queue. Only enqueue cells with the original colour. Mark visited cellscolorm x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical). You may assume all four edges of the grid are surrounded by water. The area of an island is the number of cells with a value of 1 in the island. Return the maximum area of an island in grid. If there is no island, return 0.
The grid can have multiple islands. You need to find the area (number of land cells) of the largest one. If no land exists, return 0.
Connections are only horizontal/vertical, not diagonal.
Use BFS to explore each island. When you find an unvisited land cell, add it to a queue and process neighbours level by level. Count every cell you visit. That count is the island's area.
⚙ Knowledge Hub: Using GraphsTrack the maximum area seen across all islands. Mark cells as visited (e.g. set them to 0) so you don't count them twice.
maxArea = 01, launch BFS from that cell using a queue0 (or use a visited set) and count the cellsmaxArea if this island's area is largermaxArea