|
34984 | 34984 | <ul class="md-nav__list">
|
34985 | 34985 |
|
34986 | 34986 | <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"> |
34988 | 34988 | <span class="md-ellipsis">
|
34989 |
| - Solution 1 |
| 34989 | + Solution 1: Dynamic Programming |
34990 | 34990 | </span>
|
34991 | 34991 | </a>
|
34992 | 34992 |
|
@@ -80692,11 +80692,11 @@ <h2 id="description">Description</h2>
|
80692 | 80692 | <pre>
|
80693 | 80693 | <strong>Input:</strong> word = "CAKE"
|
80694 | 80694 | <strong>Output:</strong> 3
|
80695 |
| -<strong>Explanation:</strong> Using two fingers, one optimal way to type "CAKE" is: |
80696 |
| -Finger 1 on letter 'C' -> cost = 0 |
80697 |
| -Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2 |
80698 |
| -Finger 2 on letter 'K' -> cost = 0 |
80699 |
| -Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1 |
| 80695 | +<strong>Explanation:</strong> Using two fingers, one optimal way to type "CAKE" is: |
| 80696 | +Finger 1 on letter 'C' -> cost = 0 |
| 80697 | +Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2 |
| 80698 | +Finger 2 on letter 'K' -> cost = 0 |
| 80699 | +Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1 |
80700 | 80700 | Total distance = 3
|
80701 | 80701 | </pre>
|
80702 | 80702 |
|
@@ -80727,7 +80727,19 @@ <h2 id="description">Description</h2>
|
80727 | 80727 | <h2 id="solutions">Solutions</h2>
|
80728 | 80728 | <!-- solution:start -->
|
80729 | 80729 |
|
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> |
80731 | 80743 | <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>
|
80732 | 80744 | <div class="tabbed-content">
|
80733 | 80745 | <div class="tabbed-block">
|
|
0 commit comments