Leetcode 200 Number of Islands

Problem Statement Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1 Example 2: Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] DFS: Recursive class Solution: def numIslands(self, grid: List[List[str]]) -> int: # ROWS >= COLS >= 1 ROWS, COLS = len(grid), len(grid[0]) islands = 0 # T: O(N x M) for r in range(ROWS): for c in range(COLS): if grid[r][c] == "1": islands += 1 self....

December 5, 2022 · 3 min · 483 words · Me

Leetcode 207 Course Schedule

Problem Statement There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return true if you can finish all courses....

December 3, 2022 · 2 min · 229 words · Me

Leetcode 133 Clone Graph

Problem Statement Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors. class Node { public int val; public List<Node> neighbors; } Test case format: For simplicity, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on....

December 3, 2022 · 3 min · 522 words · Me

Leetcode 733: Flood Fill

Problem Statement An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill, consider the starting pixel, plus any pixels connected >4-directionally to the starting pixel of the same color as the starting pixel, >plus any pixels connected 4-directionally to those pixels (also with the same >color), and so on....

December 3, 2022 · 3 min · 443 words · Me