圖論

圖論題型的解題教學,8 題。先判斷是走訪、連通性還是最短路徑,再決定用 BFS、DFS 還是拓撲排序。

Number of Connected Components 解題教學示意圖|LeetCode 323

Number of Connected Components 解題教學:3 種解法完整比較|LeetCode 323 Best Practice

You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph.

Return the number of connected components in the graph.

Number of Connected Components 解題教學:3 種解法完整比較|LeetCode 323 Best Practice Read More »

Leetcode, 技術文章, , ,
Graph Valid Tree 解題教學示意圖|LeetCode 261

Graph Valid Tree 解題教學:3 種解法完整比較|LeetCode 261 Best Practice

You have a graph of n nodes labeled from 0 to n – 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that there is an undirected edge between nodes ai and bi in the graph.

Return true if the edges of the given graph make up a valid tree, and false otherwise.

Graph Valid Tree 解題教學:3 種解法完整比較|LeetCode 261 Best Practice Read More »

Leetcode, 技術文章, , , ,
Alien Dictionary 解題教學示意圖|LeetCode 269

Alien Dictionary 解題教學:3 種解法完整比較|LeetCode 269 Best Practice

There is a new alien language that uses the English alphabet. However, the order of the letters is unknown to you.

You are given a list of strings words from the alien language’s dictionary. Now it is claimed that the strings in words are sorted lexicographically by the rules of this new language.

If this claim is incorrect, and the given arrangement of string in words cannot correspond to any order of letters, return “”.

Otherwise, return a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language’s rules. If there are multiple solutions, return any of them.

Alien Dictionary 解題教學:3 種解法完整比較|LeetCode 269 Best Practice Read More »

Leetcode, 技術文章, , ,
Number of Islands II 解題教學示意圖|LeetCode 305

Number of Islands II 解題教學:3 種解法完整比較|LeetCode 305 Best Practice

You are given an empty 2D binary grid grid of size m x n. The grid represents a map where 0’s represent water and 1’s represent land. Initially, all the cells of grid are water cells (i.e., all the cells are 0’s).

We may perform an add land operation which turns the water at position into a land. You are given an array positions where positions[i] = [ri, ci] is the position (ri, ci) at which we should operate the ith operation.

Return an array of integers answer where answer[i] is the number of islands after turning the cell (ri, ci) into a land.

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.

Number of Islands II 解題教學:3 種解法完整比較|LeetCode 305 Best Practice Read More »

Leetcode, 技術文章, , ,
Number of Islands 解題教學示意圖|LeetCode 200

Number of Islands 解題教學:3 種解法完整比較|LeetCode 200 Best Practice

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.

Number of Islands 解題教學:3 種解法完整比較|LeetCode 200 Best Practice Read More »

Leetcode, 技術文章, , ,
Pacific Atlantic Water Flow 解題教學示意圖|LeetCode 417

Pacific Atlantic Water Flow 解題教學:3 種解法完整比較|LeetCode 417 Best Practice

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island’s left and top edges, and the Atlantic Ocean touches the island’s right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell’s height is less than or equal to the current cell’s height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

Pacific Atlantic Water Flow 解題教學:3 種解法完整比較|LeetCode 417 Best Practice Read More »

Leetcode, 技術文章, , ,
Course Schedule 解題教學示意圖|LeetCode 207

Course Schedule 解題教學:3 種解法完整比較|LeetCode 207 Best Practice

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. Otherwise, return false.

Course Schedule 解題教學:3 種解法完整比較|LeetCode 207 Best Practice Read More »

Leetcode, 技術文章, , ,
Clone Graph 解題教學示意圖|LeetCode 133

Clone Graph 解題教學:3 種解法完整比較|LeetCode 133 Best Practice

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.

Clone Graph 解題教學:3 種解法完整比較|LeetCode 133 Best Practice Read More »

Leetcode, 技術文章, , ,