diff --git a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md index 41b9e18cce784..b51c00056551e 100644 --- a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md +++ b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md @@ -69,11 +69,13 @@ tags: ### 方法一:遍历 -我们可以遍历字符串,维护当前的嵌套深度,遇到左括号时深度加一,并且更新组最大深大;遇到右括号时深度减一。 +我们用一个变量 $d$ 记录当前的深度,初始时 $d = 0$。 -遍历结束后,返回最大深度即可。 +遍历字符串 $s$,当遇到左括号时,深度 $d$ 加一,同时更新答案为当前深度 $d$ 和答案的最大值。当遇到右括号时,深度 $d$ 减一。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。 +最后返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md index e3ee78b0b2e32..3f5fc4041f2a1 100644 --- a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md +++ b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md @@ -69,7 +69,15 @@ tags: -### 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)$. diff --git a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp index 889d1703dd6db..d85079ac5de4f 100644 --- a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp +++ b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp @@ -11,4 +11,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1600-1699/1615.Maximal Network Rank/README.md b/solution/1600-1699/1615.Maximal Network Rank/README.md index b7c57d7f63ae6..b1bff02b7e311 100644 --- a/solution/1600-1699/1615.Maximal Network Rank/README.md +++ b/solution/1600-1699/1615.Maximal Network Rank/README.md @@ -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$ 是城市的数量。 @@ -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 @@ -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; @@ -204,28 +201,4 @@ function maximalNetworkRank(n: number, roads: number[][]): number { - - -### 方法二 - - - -#### 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)) -``` - - - - - diff --git a/solution/1600-1699/1615.Maximal Network Rank/README_EN.md b/solution/1600-1699/1615.Maximal Network Rank/README_EN.md index 611ec9f1bd5a7..a80362be95a93 100644 --- a/solution/1600-1699/1615.Maximal Network Rank/README_EN.md +++ b/solution/1600-1699/1615.Maximal Network Rank/README_EN.md @@ -73,7 +73,13 @@ tags: -### 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. @@ -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 @@ -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; @@ -196,28 +199,4 @@ function maximalNetworkRank(n: number, roads: number[][]): number { - - -### Solution 2 - - - -#### 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)) -``` - - - - - diff --git a/solution/1600-1699/1615.Maximal Network Rank/Solution.ts b/solution/1600-1699/1615.Maximal Network Rank/Solution.ts index edf04a1cbb174..4dd76e1621382 100644 --- a/solution/1600-1699/1615.Maximal Network Rank/Solution.ts +++ b/solution/1600-1699/1615.Maximal Network Rank/Solution.ts @@ -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; diff --git a/solution/1600-1699/1615.Maximal Network Rank/Solution2.py b/solution/1600-1699/1615.Maximal Network Rank/Solution2.py deleted file mode 100644 index 25aadff492eab..0000000000000 --- a/solution/1600-1699/1615.Maximal Network Rank/Solution2.py +++ /dev/null @@ -1,9 +0,0 @@ -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))