Skip to content

feat: update solutions to lc problems: No.1614,1615 #3636

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
Oct 12, 2024
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 @@ -69,11 +69,13 @@ tags:

### 方法一:遍历

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

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

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
最后返回答案即可。

时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Traversal

We use a variable $d$ to record the current depth, initially $d = 0$.

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.

Finally, return the answer.

The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Solution {
}
return ans;
}
};
};
47 changes: 10 additions & 37 deletions solution/1600-1699/1615.Maximal Network Rank/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ tags:

### 方法一:计数

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

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

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

Expand All @@ -90,16 +90,13 @@ tags:
```python
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
g = defaultdict(set)
g = [[0] * n for _ in range(n)]
cnt = [0] * n
for a, b in roads:
g[a].add(b)
g[b].add(a)
ans = 0
for a in range(n):
for b in range(a + 1, n):
if (t := len(g[a]) + len(g[b]) - (a in g[b])) > ans:
ans = t
return ans
g[a][b] = g[b][a] = 1
cnt[a] += 1
cnt[b] += 1
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
```

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

```ts
function maximalNetworkRank(n: number, roads: number[][]): number {
const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
const cnt: number[] = new Array(n).fill(0);
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
const cnt: number[] = Array(n).fill(0);
for (const [a, b] of roads) {
g[a][b] = 1;
g[b][a] = 1;
Expand All @@ -204,28 +201,4 @@ function maximalNetworkRank(n: number, roads: number[][]): number {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
g = [[0] * n for _ in range(n)]
cnt = [0] * n
for a, b in roads:
g[a][b] = g[b][a] = 1
cnt[a] += 1
cnt[b] += 1
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
51 changes: 15 additions & 36 deletions solution/1600-1699/1615.Maximal Network Rank/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

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

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.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of cities.

<!-- tabs:start -->

Expand All @@ -82,16 +88,13 @@ tags:
```python
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
g = defaultdict(set)
g = [[0] * n for _ in range(n)]
cnt = [0] * n
for a, b in roads:
g[a].add(b)
g[b].add(a)
ans = 0
for a in range(n):
for b in range(a + 1, n):
if (t := len(g[a]) + len(g[b]) - (a in g[b])) > ans:
ans = t
return ans
g[a][b] = g[b][a] = 1
cnt[a] += 1
cnt[b] += 1
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
```

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

```ts
function maximalNetworkRank(n: number, roads: number[][]): number {
const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
const cnt: number[] = new Array(n).fill(0);
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
const cnt: number[] = Array(n).fill(0);
for (const [a, b] of roads) {
g[a][b] = 1;
g[b][a] = 1;
Expand All @@ -196,28 +199,4 @@ function maximalNetworkRank(n: number, roads: number[][]): number {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
g = [[0] * n for _ in range(n)]
cnt = [0] * n
for a, b in roads:
g[a][b] = g[b][a] = 1
cnt[a] += 1
cnt[b] += 1
return max(cnt[a] + cnt[b] - g[a][b] for a in range(n) for b in range(a + 1, n))
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
4 changes: 2 additions & 2 deletions solution/1600-1699/1615.Maximal Network Rank/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function maximalNetworkRank(n: number, roads: number[][]): number {
const g: number[][] = Array.from(new Array(n), () => new Array(n).fill(0));
const cnt: number[] = new Array(n).fill(0);
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
const cnt: number[] = Array(n).fill(0);
for (const [a, b] of roads) {
g[a][b] = 1;
g[b][a] = 1;
Expand Down
9 changes: 0 additions & 9 deletions solution/1600-1699/1615.Maximal Network Rank/Solution2.py

This file was deleted.

Loading