Skip to content

[pull] main from doocs:main #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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$,否则返回答案。

Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,25 @@ tags:

<!-- solution:start -->

### 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.

<!-- tabs:start -->

#### 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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ tags:

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand Down