Skip to content

Commit 0d6c8dd

Browse files
authored
feat: update solutions to lc problems: No.1761,1762 (#4055)
1 parent 933cb38 commit 0d6c8dd

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ tags:
6868

6969
### 方法一:暴力枚举
7070

71-
我们先将所有边存入邻接矩阵 $g$ 中,再将每个节点的度数存入数组 $deg$ 中。初始化答案 $ans=+\infty$。
71+
我们先将所有边存入邻接矩阵 $\textit{g}$ 中,再将每个节点的度数存入数组 $\textit{deg}$ 中。初始化答案 $\textit{ans}=+\infty$。
7272

73-
然后枚举所有的三元组 $(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)$。
73+
然后枚举所有的三元组 $(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)$。
7474

7575
枚举完所有的三元组后,如果答案仍然为 $+\infty$,说明图中不存在连通三元组,返回 $-1$,否则返回答案。
7676

@@ -81,6 +81,10 @@ tags:
8181
#### Python3
8282

8383
```python
84+
def min(a: int, b: int) -> int:
85+
return a if a < b else b
86+
87+
8488
class Solution:
8589
def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
8690
g = [[False] * n for _ in range(n)]

solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,25 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Brute Force Enumeration
68+
69+
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$.
70+
71+
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)$.
72+
73+
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.
74+
75+
The time complexity is $O(n^3)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of nodes.
6876

6977
<!-- tabs:start -->
7078

7179
#### Python3
7280

7381
```python
82+
def min(a: int, b: int) -> int:
83+
return a if a < b else b
84+
85+
7486
class Solution:
7587
def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
7688
g = [[False] * n for _ in range(n)]

solution/1700-1799/1761.Minimum Degree of a Connected Trio in a Graph/Solution.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
def min(a: int, b: int) -> int:
2+
return a if a < b else b
3+
4+
15
class Solution:
26
def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
37
g = [[False] * n for _ in range(n)]

solution/1700-1799/1762.Buildings With an Ocean View/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ tags:
7272

7373
### 方法一:逆序遍历求右侧最大值
7474

75-
我们逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。然后我们更新 $mx$ 为 $v$。
75+
我们逆序遍历数组 $\textit{height}$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $\textit{ans}$。然后我们更新 $mx$ 为 $v$。
7676

77-
遍历结束后,逆序返回 $ans$ 即可。
77+
遍历结束后,逆序返回 $\textit{ans}$ 即可。
7878

7979
时间复杂度 $O(n)$,其中 $n$ 为数组长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
8080

solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Reverse Traversal to Find the Maximum on the Right
67+
68+
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$.
69+
70+
After the traversal, return $\textit{ans}$ in reverse order.
71+
72+
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)$.
6773

6874
<!-- tabs:start -->
6975

0 commit comments

Comments
 (0)