From c7c3bc01c219d7296d6285439472fd3486263d94 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 28 Nov 2024 19:59:01 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.1319,1320 --- .../README.md | 8 ++++- .../README_EN.md | 8 ++++- .../README.md | 30 +++++++++--------- .../README_EN.md | 31 +++++++++++++++---- 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md b/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md index fcb1bce7a8efe..863cb091ee81d 100644 --- a/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md +++ b/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md @@ -79,7 +79,13 @@ tags: -### 方法一 +### 方法一:并查集 + +我们可以用并查集维护计算机之间的联通关系。遍历所有的连接,对于每个连接 $(a, b)$,如果 $a$ 和 $b$ 已经联通,那么这个连接是多余的,我们将多余的连接数加一;否则,我们将 $a$ 和 $b$ 连通,然后将联通分量数减一。 + +最后,如果联通分量数减一大于多余的连接数,说明我们无法使所有计算机联通,返回 -1;否则,返回联通分量数减一。 + +时间复杂度 $O(m \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是计算机的数量和连接的数量。 diff --git a/solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md b/solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md index 7b7bb69fe0652..48d2ba04605e9 100644 --- a/solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md +++ b/solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md @@ -70,7 +70,13 @@ tags: -### Solution 1 +### Solution 1: Union-Find + +We can use a union-find data structure to maintain the connectivity between computers. Traverse all connections, and for each connection $(a, b)$, if $a$ and $b$ are already connected, then this connection is redundant, and we increment the count of redundant connections. Otherwise, we connect $a$ and $b$, and decrement the number of connected components. + +Finally, if the number of connected components minus one is greater than the number of redundant connections, it means we cannot connect all computers, so we return -1. Otherwise, we return the number of connected components minus one. + +The time complexity is $O(m \times \log n)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the number of computers and the number of connections, respectively. diff --git a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md index bf690e32311f9..1bdffb15143d8 100644 --- a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md +++ b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md @@ -40,12 +40,12 @@ tags:
 输入:word = "CAKE"
 输出:3
-解释: 
-使用两根手指输入 "CAKE" 的最佳方案之一是: 
-手指 1 在字母 'C' 上 -> 移动距离 = 0 
-手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2 
-手指 2 在字母 'K' 上 -> 移动距离 = 0 
-手指 2 在字母 'E' 上 -> 移动距离 = 从字母 'K' 到字母 'E' 的距离  = 1 
+解释:
+使用两根手指输入 "CAKE" 的最佳方案之一是:
+手指 1 在字母 'C' 上 -> 移动距离 = 0
+手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
+手指 2 在字母 'K' 上 -> 移动距离 = 0
+手指 2 在字母 'E' 上 -> 移动距离 = 从字母 'K' 到字母 'E' 的距离  = 1
 总距离 = 3
 
@@ -81,24 +81,24 @@ tags: ### 方法一:动态规划 -我们定义 $f[i][j][k]$ 表示输入完 $word[i]$,且手指 $1$ 位于位置 $j$,手指 $2$ 位于位置 $k$ 时,最小的移动距离。这里的位置 $j$ 和 $k$ 表示的是字母对应的数字,取值范围为 $[0,..25]$。初始时 $f[i][j][k]=\infty$。 +我们定义 $f[i][j][k]$ 表示输入完 $\textit{word}[i]$,且手指 $1$ 位于位置 $j$,手指 $2$ 位于位置 $k$ 时,最小的移动距离。这里的位置 $j$ 和 $k$ 表示的是字母对应的数字,取值范围为 $[0,..25]$。初始时 $f[i][j][k]=\infty$。 -我们实现一个函数 $dist(a, b)$,表示位置 $a$ 和位置 $b$ 之间的距离,即 $dist(a, b) = |\frac{a}{6} - \frac{b}{6}| + |a \bmod 6 - b \bmod 6|$。 +我们实现一个函数 $\textit{dist}(a, b)$,表示位置 $a$ 和位置 $b$ 之间的距离,即 $\textit{dist}(a, b) = |\frac{a}{6} - \frac{b}{6}| + |a \bmod 6 - b \bmod 6|$。 -接下来,我们考虑输入 $word[0]$,即只有一个字母的的情况,此时有两种选择: +接下来,我们考虑输入 $\textit{word}[0]$,即只有一个字母的的情况,此时有两种选择: -- 手指 $1$ 位于 $word[0]$ 所在的位置,手指 $2$ 位于任意位置,此时 $f[0][word[0]][k] = 0$,其中 $k \in [0,..25]$。 -- 手指 $2$ 位于 $word[0]$ 所在的位置,手指 $1$ 位于任意位置,此时 $f[0][k][word[0]] = 0$,其中 $k \in [0,..25]$。 +- 手指 $1$ 位于 $\textit{word}[0]$ 所在的位置,手指 $2$ 位于任意位置,此时 $f[0][\textit{word}[0]][k] = 0$,其中 $k \in [0,..25]$。 +- 手指 $2$ 位于 $\textit{word}[0]$ 所在的位置,手指 $1$ 位于任意位置,此时 $f[0][k][\textit{word}[0]] = 0$,其中 $k \in [0,..25]$。 -然后我们考虑输入 $word[1,..n-1]$,我们记上一个字母和当前字母所在的位置分别为 $a$ 和 $b$,接下来我们进行分情况讨论: +然后我们考虑输入 $\textit{word}[1,..n-1]$,我们记上一个字母和当前字母所在的位置分别为 $a$ 和 $b$,接下来我们进行分情况讨论: -如果当前手指 $1$ 位于位置 $b$,我们枚举手指 $2$ 的位置 $j$,假如上一个位置 $a$ 也是手指 $1$ 的位置,那么此时有 $f[i][b][j]=\min(f[i][b][j], f[i-1][a][j]+dist(a, b))$。如果手指 $2$ 的位置与上一个位置 $a$ 相同,即 $j=a$,那么我们枚举上一个位置的手指 $1$ 所在的位置 $k$,此时有 $f[i][j][j]=\min(f[i][b][j], f[i-1][k][a]+dist(k, b))$。 +如果当前手指 $1$ 位于位置 $b$,我们枚举手指 $2$ 的位置 $j$,假如上一个位置 $a$ 也是手指 $1$ 的位置,那么此时有 $f[i][b][j]=\min(f[i][b][j], f[i-1][a][j]+\textit{dist}(a, b))$。如果手指 $2$ 的位置与上一个位置 $a$ 相同,即 $j=a$,那么我们枚举上一个位置的手指 $1$ 所在的位置 $k$,此时有 $f[i][j][j]=\min(f[i][b][j], f[i-1][k][a]+\textit{dist}(k, b))$。 -同理,如果当前手指 $2$ 位于位置 $b$,我们枚举手指 $1$ 的位置 $j$,假如上一个位置 $a$ 也是手指 $2$ 的位置,那么此时有 $f[i][j][b]=\min(f[i][j][b], f[i-1][j][a]+dist(a, b))$。如果手指 $1$ 的位置与上一个位置 $a$ 相同,即 $j=a$,那么我们枚举上一个位置的手指 $2$ 所在的位置 $k$,此时有 $f[i][j][b]=\min(f[i][j][b], f[i-1][a][k]+dist(k, b))$。 +同理,如果当前手指 $2$ 位于位置 $b$,我们枚举手指 $1$ 的位置 $j$,假如上一个位置 $a$ 也是手指 $2$ 的位置,那么此时有 $f[i][j][b]=\min(f[i][j][b], f[i-1][j][a]+\textit{dist}(a, b))$。如果手指 $1$ 的位置与上一个位置 $a$ 相同,即 $j=a$,那么我们枚举上一个位置的手指 $2$ 所在的位置 $k$,此时有 $f[i][j][b]=\min(f[i][j][b], f[i-1][a][k]+\textit{dist}(k, b))$。 最后,我们枚举最后一个位置的手指 $1$ 和手指 $2$ 所在的位置,取最小值即为答案。 -时间复杂度 $O(n \times 26^2)$,空间复杂度 $O(n \times 26^2)$。其中 $n$ 为字符串 $word$ 的长度。 +时间复杂度 $O(n \times |\Sigma|^2)$,空间复杂度 $O(n \times |\Sigma|^2)$。其中 $n$ 为字符串 $\textit{word}$ 的长度,而 $|\Sigma|$ 为字母表的大小,本题中 $|\Sigma|=26$。 diff --git a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md index 059abe5db3119..5e9170e5ba800 100644 --- a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md +++ b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md @@ -38,11 +38,11 @@ tags:
 Input: word = "CAKE"
 Output: 3
-Explanation: Using two fingers, one optimal way to type "CAKE" is: 
-Finger 1 on letter 'C' -> cost = 0 
-Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2 
-Finger 2 on letter 'K' -> cost = 0 
-Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1 
+Explanation: Using two fingers, one optimal way to type "CAKE" is:
+Finger 1 on letter 'C' -> cost = 0
+Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2
+Finger 2 on letter 'K' -> cost = 0
+Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1
 Total distance = 3
 
@@ -74,7 +74,26 @@ Total distance = 6 -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i][j][k]$ to represent the minimum distance after typing $\textit{word}[i]$, with finger 1 at position $j$ and finger 2 at position $k$. Here, positions $j$ and $k$ represent the numbers corresponding to the letters, ranging from $[0,..25]$. Initially, $f[i][j][k] = \infty$. + +We implement a function $\textit{dist}(a, b)$ to represent the distance between positions $a$ and $b$, i.e., $\textit{dist}(a, b) = |\frac{a}{6} - \frac{b}{6}| + |a \bmod 6 - b \bmod 6|$. + +Next, we consider typing $\textit{word}[0]$, i.e., the case with only one letter. There are two choices: + +- Finger 1 is at the position of $\textit{word}[0]$, and finger 2 is at any position. In this case, $f[0][\textit{word}[0]][k] = 0$, where $k \in [0,..25]$. +- Finger 2 is at the position of $\textit{word}[0]$, and finger 1 is at any position. In this case, $f[0][k][\textit{word}[0]] = 0$, where $k \in [0,..25]$. + +Then we consider typing $\textit{word}[1,..n-1]$. Let the positions of the previous letter and the current letter be $a$ and $b$, respectively. Next, we discuss the following cases: + +If the current finger 1 is at position $b$, we enumerate the position $j$ of finger 2. If the previous position $a$ was also the position of finger 1, then $f[i][b][j] = \min(f[i][b][j], f[i-1][a][j] + \textit{dist}(a, b))$. If the position of finger 2 is the same as the previous position $a$, i.e., $j = a$, then we enumerate the position $k$ of finger 1 in the previous position. In this case, $f[i][b][j] = \min(f[i][b][j], f[i-1][k][a] + \textit{dist}(k, b))$. + +Similarly, if the current finger 2 is at position $b$, we enumerate the position $j$ of finger 1. If the previous position $a$ was also the position of finger 2, then $f[i][j][b] = \min(f[i][j][b], f[i-1][j][a] + \textit{dist}(a, b))$. If the position of finger 1 is the same as the previous position $a$, i.e., $j = a$, then we enumerate the position $k$ of finger 2 in the previous position. In this case, $f[i][j][b] = \min(f[i][j][b], f[i-1][a][k] + \textit{dist}(k, b))$. + +Finally, we enumerate the positions of finger 1 and finger 2 at the last position and take the minimum value as the answer. + +The time complexity is $O(n \times |\Sigma|^2)$, and the space complexity is $O(n \times |\Sigma|^2)$. Here, $n$ is the length of the string $\textit{word}$, and $|\Sigma|$ is the size of the alphabet, which is $26$ in this problem.