Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc28c6d

Browse files
feat: add solutions to lc problems: No.1319,1320 (#3822)
1 parent b2cc6da commit dc28c6d

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

‎solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### 方法一
82+
### 方法一:并查集
83+
84+
我们可以用并查集维护计算机之间的联通关系。遍历所有的连接,对于每个连接 $(a, b),ドル如果 $a$ 和 $b$ 已经联通,那么这个连接是多余的,我们将多余的连接数加一;否则,我们将 $a$ 和 $b$ 连通,然后将联通分量数减一。
85+
86+
最后,如果联通分量数减一大于多余的连接数,说明我们无法使所有计算机联通,返回 -1;否则,返回联通分量数减一。
87+
88+
时间复杂度 $O(m \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是计算机的数量和连接的数量。
8389

8490
<!-- tabs:start -->
8591

‎solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ tags:
7070

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

73-
### Solution 1
73+
### Solution 1: Union-Find
74+
75+
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.
76+
77+
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.
78+
79+
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.
7480

7581
<!-- tabs:start -->
7682

‎solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ tags:
4040
<pre>
4141
<strong>输入:</strong>word = "CAKE"
4242
<strong>输出:</strong>3
43-
<strong>解释:
44-
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
45-
手指 1 在字母 'C' 上 -&gt; 移动距离 = 0
46-
手指 1 在字母 'A' 上 -&gt; 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
47-
手指 2 在字母 'K' 上 -&gt; 移动距离 = 0
48-
手指 2 在字母 'E' 上 -&gt; 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
43+
<strong>解释:
44+
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
45+
手指 1 在字母 'C' 上 -&gt; 移动距离 = 0
46+
手指 1 在字母 'A' 上 -&gt; 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
47+
手指 2 在字母 'K' 上 -&gt; 移动距离 = 0
48+
手指 2 在字母 'E' 上 -&gt; 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
4949
总距离 = 3
5050
</pre>
5151

@@ -81,24 +81,24 @@ tags:
8181

8282
### 方法一:动态规划
8383

84-
我们定义 $f[i][j][k]$ 表示输入完 $word[i],ドル且手指 1ドル$ 位于位置 $j,ドル手指 2ドル$ 位于位置 $k$ 时,最小的移动距离。这里的位置 $j$ 和 $k$ 表示的是字母对应的数字,取值范围为 $[0,..25]$。初始时 $f[i][j][k]=\infty$。
84+
我们定义 $f[i][j][k]$ 表示输入完 $\textit{word}[i],ドル且手指 1ドル$ 位于位置 $j,ドル手指 2ドル$ 位于位置 $k$ 时,最小的移动距离。这里的位置 $j$ 和 $k$ 表示的是字母对应的数字,取值范围为 $[0,..25]$。初始时 $f[i][j][k]=\infty$。
8585

86-
我们实现一个函数 $dist(a, b),ドル表示位置 $a$ 和位置 $b$ 之间的距离,即 $dist(a, b) = |\frac{a}{6} - \frac{b}{6}| + |a \bmod 6 - b \bmod 6|$。
86+
我们实现一个函数 $\textit{dist}(a, b),ドル表示位置 $a$ 和位置 $b$ 之间的距离,即 $\textit{dist}(a, b) = |\frac{a}{6} - \frac{b}{6}| + |a \bmod 6 - b \bmod 6|$。
8787

88-
接下来,我们考虑输入 $word[0],ドル即只有一个字母的的情况,此时有两种选择:
88+
接下来,我们考虑输入 $\textit{word}[0],ドル即只有一个字母的的情况,此时有两种选择:
8989

90-
- 手指 1ドル$ 位于 $word[0]$ 所在的位置,手指 2ドル$ 位于任意位置,此时 $f[0][word[0]][k] = 0,ドル其中 $k \in [0,..25]$。
91-
- 手指 2ドル$ 位于 $word[0]$ 所在的位置,手指 1ドル$ 位于任意位置,此时 $f[0][k][word[0]] = 0,ドル其中 $k \in [0,..25]$。
90+
- 手指 1ドル$ 位于 $\textit{word}[0]$ 所在的位置,手指 2ドル$ 位于任意位置,此时 $f[0][\textit{word}[0]][k] = 0,ドル其中 $k \in [0,..25]$。
91+
- 手指 2ドル$ 位于 $\textit{word}[0]$ 所在的位置,手指 1ドル$ 位于任意位置,此时 $f[0][k][\textit{word}[0]] = 0,ドル其中 $k \in [0,..25]$。
9292

93-
然后我们考虑输入 $word[1,..n-1],ドル我们记上一个字母和当前字母所在的位置分别为 $a$ 和 $b,ドル接下来我们进行分情况讨论:
93+
然后我们考虑输入 $\textit{word}[1,..n-1],ドル我们记上一个字母和当前字母所在的位置分别为 $a$ 和 $b,ドル接下来我们进行分情况讨论:
9494

95-
如果当前手指 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))$。
95+
如果当前手指 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))$。
9696

97-
同理,如果当前手指 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))$。
97+
同理,如果当前手指 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))$。
9898

9999
最后,我们枚举最后一个位置的手指 1ドル$ 和手指 2ドル$ 所在的位置,取最小值即为答案。
100100

101-
时间复杂度 $O(n \times 26^2),ドル空间复杂度 $O(n \times 26^2)$。其中 $n$ 为字符串 $word$ 的长度。
101+
时间复杂度 $O(n \times |\Sigma|^2),ドル空间复杂度 $O(n \times |\Sigma|^2)$。其中 $n$ 为字符串 $\textit{word}$ 的长度,而 $|\Sigma|$ 为字母表的大小,本题中 $|\Sigma|=26$
102102

103103
<!-- tabs:start -->
104104

‎solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ tags:
3838
<pre>
3939
<strong>Input:</strong> word = &quot;CAKE&quot;
4040
<strong>Output:</strong> 3
41-
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
42-
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
43-
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
44-
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
45-
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
41+
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
42+
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
43+
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
44+
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
45+
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
4646
Total distance = 3
4747
</pre>
4848

@@ -74,7 +74,26 @@ Total distance = 6
7474

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

77-
### Solution 1
77+
### Solution 1: Dynamic Programming
78+
79+
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$.
80+
81+
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|$.
82+
83+
Next, we consider typing $\textit{word}[0],ドル i.e., the case with only one letter. There are two choices:
84+
85+
- 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]$.
86+
- 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]$.
87+
88+
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:
89+
90+
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))$.
91+
92+
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))$.
93+
94+
Finally, we enumerate the positions of finger 1 and finger 2 at the last position and take the minimum value as the answer.
95+
96+
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.
7897

7998
<!-- tabs:start -->
8099

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /