Skip to content

Commit ce50f05

Browse files
committed
deploy: 8cbbd41
1 parent 1dd1332 commit ce50f05

File tree

14 files changed

+7505
-7329
lines changed

14 files changed

+7505
-7329
lines changed

en/lc/1319/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34963,9 +34963,9 @@
3496334963
<ul class="md-nav__list">
3496434964

3496534965
<li class="md-nav__item">
34966-
<a href="#solution-1" class="md-nav__link">
34966+
<a href="#solution-1-union-find" class="md-nav__link">
3496734967
<span class="md-ellipsis">
34968-
Solution 1
34968+
Solution 1: Union-Find
3496934969
</span>
3497034970
</a>
3497134971

@@ -80733,7 +80733,10 @@ <h2 id="description">Description</h2>
8073380733
<h2 id="solutions">Solutions</h2>
8073480734
<!-- solution:start -->
8073580735

80736-
<h3 id="solution-1">Solution 1</h3>
80736+
<h3 id="solution-1-union-find">Solution 1: Union-Find</h3>
80737+
<p>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.</p>
80738+
<p>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.</p>
80739+
<p>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.</p>
8073780740
<div class="tabbed-set tabbed-alternate" data-tabs="1:5"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label></div>
8073880741
<div class="tabbed-content">
8073980742
<div class="tabbed-block">

en/lc/1320/index.html

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34984,9 +34984,9 @@
3498434984
<ul class="md-nav__list">
3498534985

3498634986
<li class="md-nav__item">
34987-
<a href="#solution-1" class="md-nav__link">
34987+
<a href="#solution-1-dynamic-programming" class="md-nav__link">
3498834988
<span class="md-ellipsis">
34989-
Solution 1
34989+
Solution 1: Dynamic Programming
3499034990
</span>
3499134991
</a>
3499234992

@@ -80692,11 +80692,11 @@ <h2 id="description">Description</h2>
8069280692
<pre>
8069380693
<strong>Input:</strong> word = &quot;CAKE&quot;
8069480694
<strong>Output:</strong> 3
80695-
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
80696-
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
80697-
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
80698-
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
80699-
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
80695+
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
80696+
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
80697+
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
80698+
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
80699+
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
8070080700
Total distance = 3
8070180701
</pre>
8070280702

@@ -80727,7 +80727,19 @@ <h2 id="description">Description</h2>
8072780727
<h2 id="solutions">Solutions</h2>
8072880728
<!-- solution:start -->
8072980729

80730-
<h3 id="solution-1">Solution 1</h3>
80730+
<h3 id="solution-1-dynamic-programming">Solution 1: Dynamic Programming</h3>
80731+
<p>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$.</p>
80732+
<p>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|$.</p>
80733+
<p>Next, we consider typing $\textit{word}[0]$, i.e., the case with only one letter. There are two choices:</p>
80734+
<ul>
80735+
<li>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]$.</li>
80736+
<li>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]$.</li>
80737+
</ul>
80738+
<p>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:</p>
80739+
<p>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))$.</p>
80740+
<p>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))$.</p>
80741+
<p>Finally, we enumerate the positions of finger 1 and finger 2 at the last position and take the minimum value as the answer.</p>
80742+
<p>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.</p>
8073180743
<div class="tabbed-set tabbed-alternate" data-tabs="1:4"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label></div>
8073280744
<div class="tabbed-content">
8073380745
<div class="tabbed-block">

0 commit comments

Comments
 (0)