diff --git a/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README.md b/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README.md index b76044df49ef4..90006eafe2d23 100644 --- a/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README.md +++ b/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README.md @@ -68,9 +68,9 @@ tags: ### 方法一:暴力枚举 -我们先将所有边存入邻接矩阵 $g$ 中,再将每个节点的度数存入数组 $deg$ 中。初始化答案 $ans=+\infty$。 +我们先将所有边存入邻接矩阵 $\textit{g}$ 中,再将每个节点的度数存入数组 $\textit{deg}$ 中。初始化答案 $\textit{ans}=+\infty$。 -然后枚举所有的三元组 $(i, j, k)$,其中 $i \lt j \lt k$,如果 $g[i][j] = g[j][k] = g[i][k] = 1$,则说明这三个节点构成了一个连通三元组,此时更新答案为 $ans = \min(ans, deg[i] + deg[j] + deg[k] - 6)$。 +然后枚举所有的三元组 $(i, j, k)$,其中 $i \lt j \lt k$,如果 $\textit{g}[i][j] = \textit{g}[j][k] = \textit{g}[i][k] = 1$,则说明这三个节点构成了一个连通三元组,此时更新答案为 $\textit{ans} = \min(\textit{ans}, \textit{deg}[i] + \textit{deg}[j] + \textit{deg}[k] - 6)$。 枚举完所有的三元组后,如果答案仍然为 $+\infty$,说明图中不存在连通三元组,返回 $-1$,否则返回答案。 @@ -81,6 +81,10 @@ tags: #### Python3 ```python +def min(a: int, b: int) -> int: + return a if a < b else b + + class Solution: def minTrioDegree(self, n: int, edges: List[List[int]]) -> int: g = [[False] * n for _ in range(n)] diff --git a/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README_EN.md b/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README_EN.md index 97e128ac2fbb1..5fef7b1a02c31 100644 --- a/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README_EN.md +++ b/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README_EN.md @@ -64,13 +64,25 @@ tags: -### Solution 1 +### Solution 1: Brute Force Enumeration + +We first store all edges in the adjacency matrix $\textit{g}$, and then store the degree of each node in the array $\textit{deg}$. Initialize the answer $\textit{ans} = +\infty$. + +Then enumerate all triplets $(i, j, k)$, where $i \lt j \lt k$. If $\textit{g}[i][j] = \textit{g}[j][k] = \textit{g}[i][k] = 1$, it means these three nodes form a connected trio. In this case, update the answer to $\textit{ans} = \min(\textit{ans}, \textit{deg}[i] + \textit{deg}[j] + \textit{deg}[k] - 6)$. + +After enumerating all triplets, if the answer is still $+\infty$, it means there is no connected trio in the graph, return $-1$. Otherwise, return the answer. + +The time complexity is $O(n^3)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of nodes. #### Python3 ```python +def min(a: int, b: int) -> int: + return a if a < b else b + + class Solution: def minTrioDegree(self, n: int, edges: List[List[int]]) -> int: g = [[False] * n for _ in range(n)] diff --git a/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/Solution.py b/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/Solution.py index d1374fbbade6c..c60c13a922c49 100644 --- a/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/Solution.py +++ b/solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/Solution.py @@ -1,3 +1,7 @@ +def min(a: int, b: int) -> int: + return a if a < b else b + + class Solution: def minTrioDegree(self, n: int, edges: List[List[int]]) -> int: g = [[False] * n for _ in range(n)] diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/README.md b/solution/1700-1799/1762.Buildings With an Ocean View/README.md index 59c911016611d..eb5d567fc08f6 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/README.md +++ b/solution/1700-1799/1762.Buildings With an Ocean View/README.md @@ -72,9 +72,9 @@ tags: ### 方法一:逆序遍历求右侧最大值 -我们逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。然后我们更新 $mx$ 为 $v$。 +我们逆序遍历数组 $\textit{height}$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $\textit{ans}$。然后我们更新 $mx$ 为 $v$。 -遍历结束后,逆序返回 $ans$ 即可。 +遍历结束后,逆序返回 $\textit{ans}$ 即可。 时间复杂度 $O(n)$,其中 $n$ 为数组长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 diff --git a/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md b/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md index 1575f307f3fd0..2967549455325 100644 --- a/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md +++ b/solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md @@ -63,7 +63,13 @@ tags: -### Solution 1 +### Solution 1: Reverse Traversal to Find the Maximum on the Right + +We traverse the array $\textit{height}$ in reverse order for each element $v$, comparing $v$ with the maximum element $mx$ on the right. If $mx \lt v$, it means all elements to the right are smaller than the current element, so the current position can see the ocean and is added to the result array $\textit{ans}$. Then we update $mx$ to $v$. + +After the traversal, return $\textit{ans}$ in reverse order. + +The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.