Leetcode-cn.com 2022-04-06
🟡 310.minimum-height-trees
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Description
树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。
给你一棵包含
可选择树中任何一个节点作为根。当选择节点
请你找到所有的 最小高度树 并按 任意顺序 返回它们的根节点标签列表。
树的 高度 是指根节点和叶子节点之间最长向下路径上边的数量。
Example
🟡 310.minimum-height-trees
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Description
树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。
给你一棵包含
n 个节点的树,标记为 0 到 n - 1 。给定数字 n 和一个有 n - 1 条无向边的 edges 列表(每一个边都是一对标签),其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 之间存在一条无向边。可选择树中任何一个节点作为根。当选择节点
x 作为根节点时,设结果树的高度为 h 。在所有可能的树中,具有最小高度的树(即,min(h))被称为 最小高度树 。请你找到所有的 最小高度树 并按 任意顺序 返回它们的根节点标签列表。
树的 高度 是指根节点和叶子节点之间最长向下路径上边的数量。
Example
输入:n = 4, edges = [[1,0],[1,2],[1,3]]
输出:[1]
解释:如图所示,当根是标签为 1 的节点时,树的高度是 1 ,这是唯一的最小高度树。
Leetcode-cn.com 2022-05-31
🔴 剑指 Offer II 114.Jf1JuT
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort #array #string
Description
现有一种使用英语字母的外星文语言,这门语言的字母顺序与英语顺序不同。
给定一个字符串列表
请你根据该词典还原出此语言中已知的字母顺序,并 按字母递增顺序 排列。若不存在合法字母顺序,返回
字符串
在第一个不同字母处,如果
如果前面
Example
🔴 剑指 Offer II 114.Jf1JuT
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort #array #string
Description
现有一种使用英语字母的外星文语言,这门语言的字母顺序与英语顺序不同。
给定一个字符串列表
words ,作为这门语言的词典,words 中的字符串已经 按这门新语言的字母顺序进行了排序 。请你根据该词典还原出此语言中已知的字母顺序,并 按字母递增顺序 排列。若不存在合法字母顺序,返回
"" 。若存在多种可能的合法字母顺序,返回其中 任意一种 顺序即可。字符串
s 字典顺序小于 字符串 t 有两种情况:在第一个不同字母处,如果
s 中的字母在这门外星语言的字母顺序中位于 t 中字母之前,那么 s 的字典顺序小于 t 。如果前面
min(s.length, t.length) 字母都相同,那么 s.length < t.length 时,s 的字典顺序也小于 t 。Example
输入:words = ["wrt","wrf","er","ett","rftt"]
输出:"wertf"
2246.longest-path-with-different-adjacent-characters.pdf
91.2 KB
leetcode.com 2023-01-13
🔴2246.longest-path-with-different-adjacent-characters
🏷️ Tags
#array #string #tree #depth_first_search #graph #topological_sort
🔴2246.longest-path-with-different-adjacent-characters
🏷️ Tags
#array #string #tree #depth_first_search #graph #topological_sort
1632.rank-transform-of-a-matrix.pdf
156.9 KB
leetcode.cn 2023-01-25
🔴1632.rank-transform-of-a-matrix
🏷️ Tags
#greedy #union_find #graph #topological_sort #array #matrix
🔴1632.rank-transform-of-a-matrix
🏷️ Tags
#greedy #union_find #graph #topological_sort #array #matrix
2360.longest-cycle-in-a-graph.pdf
86.5 KB
leetcode.com 2023-03-26
🔴2360.longest-cycle-in-a-graph
🏷️ Tags
#depth_first_search #graph #topological_sort
🔴2360.longest-cycle-in-a-graph
🏷️ Tags
#depth_first_search #graph #topological_sort
1857.largest-color-value-in-a-directed-graph.pdf
92.3 KB
leetcode.com 2023-04-09
🔴1857.largest-color-value-in-a-directed-graph
🏷️ Tags
#hash_table #dynamic_programming #graph #topological_sort #memoization #counting
🔴1857.largest-color-value-in-a-directed-graph
🏷️ Tags
#hash_table #dynamic_programming #graph #topological_sort #memoization #counting
leetcode.com 2023-06-18
🔴2328.number-of-increasing-paths-in-a-grid
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort #memoization #array #dynamic_programming #matrix
🔴2328.number-of-increasing-paths-in-a-grid
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort #memoization #array #dynamic_programming #matrix
Telegraph
number-of-increasing-paths-in-a-grid
You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions. Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may…
leetcode.com 2023-07-12
🟡802.find-eventual-safe-states
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
🟡802.find-eventual-safe-states
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Telegraph
find-eventual-safe-states
There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in…
leetcode.com 2023-07-13
🟡207.course-schedule
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
🟡207.course-schedule
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Telegraph
course-schedule
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.
leetcode.cn 2023-07-28
🔴2050.parallel-courses-iii
🏷️ Tags
#graph #topological_sort #array #dynamic_programming
🔴2050.parallel-courses-iii
🏷️ Tags
#graph #topological_sort #array #dynamic_programming
Telegraph
parallel-courses-iii
给你一个整数 n ,表示有 n 节课,课程编号从 1 到 n 。同时给你一个二维整数数组 relations ,其中 relations[j] = [prevCoursej, nextCoursej] ,表示课程 prevCoursej 必须在课程 nextCoursej 之前 完成(先修课的关系)。同时给你一个下标从 0 开始的整数数组 time ,其中 time[i] 表示完成第 (i+1) 门课程需要花费的 月份 数。 请你根据以下规则算出完成所有课程所需要的 最少 月份数:
leetcode.com 2023-08-20
🔴1203.sort-items-by-groups-respecting-dependencies
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
🔴1203.sort-items-by-groups-respecting-dependencies
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Telegraph
sort-items-by-groups-respecting-dependencies
There are n items each belonging to zero or one of m groups where group[i] is the group that the i-th item belongs to and it's equal to -1 if the i-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging…
leetcode.com 2023-10-18
🔴2050.parallel-courses-iii
🏷️ Tags
#graph #topological_sort #array #dynamic_programming
🔴2050.parallel-courses-iii
🏷️ Tags
#graph #topological_sort #array #dynamic_programming
Telegraph
parallel-courses-iii
You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that course prevCoursej has to be completed before course nextCoursej…
leetcode.cn 2023-11-01
🔴2127.maximum-employees-to-be-invited-to-a-meeting
🏷️ Tags
#depth_first_search #graph #topological_sort
🔴2127.maximum-employees-to-be-invited-to-a-meeting
🏷️ Tags
#depth_first_search #graph #topological_sort
Telegraph
maximum-employees-to-be-invited-to-a-meeting
一个公司准备组织一场会议,邀请名单上有 n 位员工。公司准备了一张 圆形 的桌子,可以坐下 任意数目 的员工。 员工编号为 0 到 n - 1 。每位员工都有一位 喜欢 的员工,每位员工 当且仅当 他被安排在喜欢员工的旁边,他才会参加会议。每位员工喜欢的员工 不会 是他自己。 给你一个下标从 0 开始的整数数组 favorite ,其中 favorite[i] 表示第 i 位员工喜欢的员工。请你返回参加会议的 最多员工数目 。 示例 1: 输入:favorite = [2,2,1,2] 输出:3 解释:…
leetcode.com 2025-03-21
🟡2115.find-all-possible-recipes-from-given-supplies
🏷️ Tags
#graph #topological_sort #array #hash_table #string
🟡2115.find-all-possible-recipes-from-given-supplies
🏷️ Tags
#graph #topological_sort #array #hash_table #string
Telegraph
find-all-possible-recipes-from-given-supplies
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can…
leetcode.cn 2025-03-29
🔴2360.longest-cycle-in-a-graph
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
🔴2360.longest-cycle-in-a-graph
🏷️ Tags
#depth_first_search #breadth_first_search #graph #topological_sort
Telegraph
longest-cycle-in-a-graph
给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,其中每个节点 至多 有一条出边。 图用一个大小为 n 下标从 0 开始的数组 edges 表示,节点 i 到节点 edges[i] 之间有一条有向边。如果节点 i 没有出边,那么 edges[i] == -1 。 请你返回图中的 最长 环,如果没有任何环,请返回 -1 。 一个环指的是起点和终点是 同一个 节点的路径。 示例 1: 输入:edges = [3,3,4,2,3] 输出去:3 解释:图中的最长环是:2 -> 4 -> 3…
leetcode.cn 2025-05-26
🔴1857.largest-color-value-in-a-directed-graph
🏷️ Tags
#graph #topological_sort #memoization #hash_table #dynamic_programming #counting
🔴1857.largest-color-value-in-a-directed-graph
🏷️ Tags
#graph #topological_sort #memoization #hash_table #dynamic_programming #counting
Telegraph
largest-color-value-in-a-directed-graph
给你一个 有向图 ,它含有 n 个节点和 m 条边。节点编号从 0 到 n - 1 。 给你一个字符串 colors ,其中 colors[i] 是小写英文字母,表示图中第 i 个节点的 颜色 (下标从 0 开始)。同时给你一个二维数组 edges ,其中 edges[j] = [aj, bj] 表示从节点 aj 到节点 bj 有一条 有向边 。 图中一条有效 路径 是一个点序列 x1 -> x2 -> x3 -> ... -> xk ,对于所有 1 <= i < k ,从 xi 到 xi+1 在图中有一条有向边。路径的…