Coding Patterns

Course Schedule

You are given a total number of courses (n) and a list of prerequisites represented as pairs of course numbers. The prerequisites indicate that to take course prerequisites, you must…

Minesweeper

You are given a 2D grid representing a Minesweeper game. You need to reveal the cells of the grid according to the following rules:

Count Sub Islands

You are given two 2D grids, grid1 and grid2, both representing islands, where grid1 represents the original island and grid2 represents a sub-island formed by cutting out a rectangle from…

Find All Groups of Farmland

You are given a 2D grid representing a piece of farmland, where the value 1 represents cultivated land and 0 represents uncultivated land. A group of farmland is a connected…

Walls and Gates

You are given a 2D grid representing a house, with rooms labeled as 'INF' for empty rooms, -1 for walls, and 0 for gates. You need to fill each empty…

Surrounded Regions

Given a 2D board containing 'X' (representing walls) and 'O' (representing open spaces), capture all regions surrounded by 'X'. A region is considered surrounded if all its adjacent cells (top,…

Number of Islands

Given a 2D grid of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or…

Flood Fill

Given an image represented by a 2D matrix of integers, an initial pixel, and a new color, implement the flood fill algorithm to change all connected pixels with the initial…

1.2) Binary Tree Level Order Traversal II

Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. The bottom-up level order traversal means visiting all nodes at each level from…