Skip to content

Commit 497b411

Browse files
authored
feat: update solutions to lc problems: No.1614,1615 (doocs#3636)
1 parent 0ec4c29 commit 497b411

File tree

7 files changed

+42
-89
lines changed

7 files changed

+42
-89
lines changed

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ tags:
6969

7070
### 方法一:遍历
7171

72-
我们可以遍历字符串,维护当前的嵌套深度,遇到左括号时深度加一,并且更新组最大深大;遇到右括号时深度减一
72+
我们用一个变量 $d$ 记录当前的深度,初始时 $d = 0$
7373

74-
遍历结束后,返回最大深度即可
74+
遍历字符串 $s$,当遇到左括号时,深度 $d$ 加一,同时更新答案为当前深度 $d$ 和答案的最大值。当遇到右括号时,深度 $d$ 减一
7575

76-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
76+
最后返回答案即可。
77+
78+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
7779

7880
<!-- tabs:start -->
7981

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ tags:
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Traversal
73+
74+
We use a variable $d$ to record the current depth, initially $d = 0$.
75+
76+
Traverse the string $s$. When encountering a left parenthesis, increment the depth $d$ by one and update the answer to be the maximum of the current depth $d$ and the answer. When encountering a right parenthesis, decrement the depth $d$ by one.
77+
78+
Finally, return the answer.
79+
80+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
7381

7482
<!-- tabs:start -->
7583

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ class Solution {
1111
}
1212
return ans;
1313
}
14-
};
14+
};

solution/1600-1699/1615.Maximal Network Rank/README.md

+10-37
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ tags:
7777

7878
### 方法一:计数
7979

80-
我们可以用一维数组 $cnt$ 记录每个城市的度,用二维数组 $g$ 记录每对城市之间是否有道路相连,如果城市 $a$ 和城市 $b$ 之间有道路相连,则 $g[a][b] = g[b][a] = 1$,否则 $g[a][b] = g[b][a] = 0$。
80+
我们可以用一维数组 $\textit{cnt}$ 记录每个城市的度,用二维数组 $\textit{g}$ 记录每对城市之间是否有道路相连,如果城市 $a$ 和城市 $b$ 之间有道路相连,则 $\textit{g}[a][b] = \textit{g}[b][a] = 1$,否则 $\textit{g}[a][b] = \textit{g}[b][a] = 0$。
8181

82-
接下来,我们枚举每对城市 $(a, b)$,其中 $a \lt b$,计算它们的网络秩,即 $cnt[a] + cnt[b] - g[a][b]$,取其中的最大值即为答案。
82+
接下来,我们枚举每对城市 $(a, b)$,其中 $a \lt b$,计算它们的网络秩,即 $\textit{cnt}[a] + \textit{cnt}[b] - \textit{g}[a][b]$,取其中的最大值即为答案。
8383

8484
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是城市的数量。
8585

@@ -90,16 +90,13 @@ tags:
9090
```python
9191
class Solution:
9292
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
93-
g = defaultdict(set)
93+
g = [[0] * n for _ in range(n)]
94+
cnt = [0] * n
9495
for a, b in roads:
95-
g[a].add(b)
96-
g[b].add(a)
97-
ans = 0
98-
for a in range(n):
99-
for b in range(a + 1, n):
100-
if (t := len(g[a]) + len(g[b]) - (a in g[b])) > ans:
101-
ans = t
102-
return ans
96+
g[a][b] = g[b][a] = 1
97+
cnt[a] += 1
98+
cnt[b] += 1
99+
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
103100
```
104101

105102
#### Java
@@ -182,8 +179,8 @@ func maximalNetworkRank(n int, roads [][]int) (ans int) {
182179

183180
```ts
184181
function maximalNetworkRank(n: number, roads: number[][]): number {
185-
const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
186-
const cnt: number[] = new Array(n).fill(0);
182+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
183+
const cnt: number[] = Array(n).fill(0);
187184
for (const [a, b] of roads) {
188185
g[a][b] = 1;
189186
g[b][a] = 1;
@@ -204,28 +201,4 @@ function maximalNetworkRank(n: number, roads: number[][]): number {
204201

205202
<!-- solution:end -->
206203

207-
<!-- solution:start -->
208-
209-
### 方法二
210-
211-
<!-- tabs:start -->
212-
213-
#### Python3
214-
215-
```python
216-
class Solution:
217-
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
218-
g = [[0] * n for _ in range(n)]
219-
cnt = [0] * n
220-
for a, b in roads:
221-
g[a][b] = g[b][a] = 1
222-
cnt[a] += 1
223-
cnt[b] += 1
224-
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
225-
```
226-
227-
<!-- tabs:end -->
228-
229-
<!-- solution:end -->
230-
231204
<!-- problem:end -->

solution/1600-1699/1615.Maximal Network Rank/README_EN.md

+15-36
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ tags:
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Counting
77+
78+
We can use a one-dimensional array $\textit{cnt}$ to record the degree of each city and a two-dimensional array $\textit{g}$ to record whether there is a road between each pair of cities. If there is a road between city $a$ and city $b$, then $\textit{g}[a][b] = \textit{g}[b][a] = 1$; otherwise, $\textit{g}[a][b] = \textit{g}[b][a] = 0$.
79+
80+
Next, we enumerate each pair of cities $(a, b)$, where $a \lt b$, and calculate their network rank, which is $\textit{cnt}[a] + \textit{cnt}[b] - \textit{g}[a][b]$. The maximum value among these is the answer.
81+
82+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of cities.
7783

7884
<!-- tabs:start -->
7985

@@ -82,16 +88,13 @@ tags:
8288
```python
8389
class Solution:
8490
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
85-
g = defaultdict(set)
91+
g = [[0] * n for _ in range(n)]
92+
cnt = [0] * n
8693
for a, b in roads:
87-
g[a].add(b)
88-
g[b].add(a)
89-
ans = 0
90-
for a in range(n):
91-
for b in range(a + 1, n):
92-
if (t := len(g[a]) + len(g[b]) - (a in g[b])) > ans:
93-
ans = t
94-
return ans
94+
g[a][b] = g[b][a] = 1
95+
cnt[a] += 1
96+
cnt[b] += 1
97+
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
9598
```
9699

97100
#### Java
@@ -174,8 +177,8 @@ func maximalNetworkRank(n int, roads [][]int) (ans int) {
174177

175178
```ts
176179
function maximalNetworkRank(n: number, roads: number[][]): number {
177-
const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
178-
const cnt: number[] = new Array(n).fill(0);
180+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
181+
const cnt: number[] = Array(n).fill(0);
179182
for (const [a, b] of roads) {
180183
g[a][b] = 1;
181184
g[b][a] = 1;
@@ -196,28 +199,4 @@ function maximalNetworkRank(n: number, roads: number[][]): number {
196199

197200
<!-- solution:end -->
198201

199-
<!-- solution:start -->
200-
201-
### Solution 2
202-
203-
<!-- tabs:start -->
204-
205-
#### Python3
206-
207-
```python
208-
class Solution:
209-
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
210-
g = [[0] * n for _ in range(n)]
211-
cnt = [0] * n
212-
for a, b in roads:
213-
g[a][b] = g[b][a] = 1
214-
cnt[a] += 1
215-
cnt[b] += 1
216-
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
217-
```
218-
219-
<!-- tabs:end -->
220-
221-
<!-- solution:end -->
222-
223202
<!-- problem:end -->

solution/1600-1699/1615.Maximal Network Rank/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function maximalNetworkRank(n: number, roads: number[][]): number {
2-
const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
3-
const cnt: number[] = new Array(n).fill(0);
2+
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
3+
const cnt: number[] = Array(n).fill(0);
44
for (const [a, b] of roads) {
55
g[a][b] = 1;
66
g[b][a] = 1;

solution/1600-1699/1615.Maximal Network Rank/Solution2.py

-9
This file was deleted.

0 commit comments

Comments
 (0)