Skip to content

Commit b382c51

Browse files
committed
deploy: 8e2a2e6
1 parent 93ddf94 commit b382c51

File tree

15 files changed

+1735
-219
lines changed

15 files changed

+1735
-219
lines changed

en/lc/146/index.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9860,9 +9860,9 @@
98609860
<ul class="md-nav__list">
98619861

98629862
<li class="md-nav__item">
9863-
<a href="#solution-1" class="md-nav__link">
9863+
<a href="#solution-1-hash-table-doubly-linked-list" class="md-nav__link">
98649864
<span class="md-ellipsis">
9865-
Solution 1
9865+
Solution 1: Hash Table + Doubly Linked List
98669866
</span>
98679867
</a>
98689868

@@ -75343,7 +75343,15 @@ <h2 id="description">Description</h2>
7534375343
</ul>
7534475344

7534575345
<h2 id="solutions">Solutions</h2>
75346-
<h3 id="solution-1">Solution 1</h3>
75346+
<h3 id="solution-1-hash-table-doubly-linked-list">Solution 1: Hash Table + Doubly Linked List</h3>
75347+
<p>We can implement an LRU (Least Recently Used) cache using a "hash table" and a "doubly linked list".</p>
75348+
<ul>
75349+
<li>Hash Table: Used to store the key and its corresponding node ___location.</li>
75350+
<li>Doubly Linked List: Used to store node data, sorted by access time.</li>
75351+
</ul>
75352+
<p>When accessing a node, if the node exists, we delete it from its original position and reinsert it at the head of the list. This ensures that the node stored at the tail of the list is the least recently used node. When the number of nodes exceeds the maximum cache space, we eliminate the node at the tail of the list.</p>
75353+
<p>When inserting a node, if the node exists, we delete it from its original position and reinsert it at the head of the list. If it does not exist, we first check if the cache is full. If it is full, we delete the node at the tail of the list and insert the new node at the head of the list.</p>
75354+
<p>The time complexity is $O(1)$, and the space complexity is $O(\text{capacity})$.</p>
7534775355
<div class="tabbed-set tabbed-alternate" data-tabs="1:7"><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" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" 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><label for="__tabbed_1_6">Rust</label><label for="__tabbed_1_7">C#</label></div>
7534875356
<div class="tabbed-content">
7534975357
<div class="tabbed-block">

en/lcci/16.1/index.html

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74183,15 +74183,6 @@
7418374183
</span>
7418474184
</a>
7418574185

74186-
</li>
74187-
74188-
<li class="md-nav__item">
74189-
<a href="#solution-2" class="md-nav__link">
74190-
<span class="md-ellipsis">
74191-
Solution 2
74192-
</span>
74193-
</a>
74194-
7419574186
</li>
7419674187

7419774188
</ul>
@@ -75394,21 +75385,6 @@ <h3 id="solution-1-bitwise-operation">Solution 1: Bitwise Operation</h3>
7539475385
</div>
7539575386
</div>
7539675387
</div>
75397-
<h3 id="solution-2">Solution 2</h3>
75398-
<div class="tabbed-set tabbed-alternate" data-tabs="2:1"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">TypeScript</label></div>
75399-
<div class="tabbed-content">
75400-
<div class="tabbed-block">
75401-
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
75402-
<span class="normal">2</span>
75403-
<span class="normal">3</span>
75404-
<span class="normal">4</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="kd">function</span><span class="w"> </span><span class="nx">swapNumbers</span><span class="p">(</span><span class="nx">numbers</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[])</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
75405-
<span class="w"> </span><span class="p">[</span><span class="nx">numbers</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span><span class="w"> </span><span class="nx">numbers</span><span class="p">[</span><span class="mf">1</span><span class="p">]]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">numbers</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span><span class="w"> </span><span class="nx">numbers</span><span class="p">[</span><span class="mf">0</span><span class="p">]];</span>
75406-
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">numbers</span><span class="p">;</span>
75407-
<span class="p">}</span>
75408-
</code></pre></div></td></tr></table></div>
75409-
</div>
75410-
</div>
75411-
</div>
7541275388
<!-- end -->
7541375389

7541475390

en/lcci/16.20/index.html

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74561,15 +74561,6 @@
7456174561
</span>
7456274562
</a>
7456374563

74564-
</li>
74565-
74566-
<li class="md-nav__item">
74567-
<a href="#solution-2" class="md-nav__link">
74568-
<span class="md-ellipsis">
74569-
Solution 2
74570-
</span>
74571-
</a>
74572-
7457374564
</li>
7457474565

7457574566
</ul>
@@ -75305,7 +75296,7 @@ <h3 id="solution-1-reverse-thinking">Solution 1: Reverse Thinking</h3>
7530575296
<p>We consider a forward solution, which traverses each digit in the string $num$, maps it to the corresponding letter, combines all the letters to obtain all possible words, and then compares them with the given word list. If the word is in the list, it is added to the answer. The time complexity of this solution is $O(4^n)$, where $n$ is the length of the string $num$, which will obviously time out.</p>
7530675297
<p>Instead, we can consider a reverse solution, which traverses the given word list, and for each word $w$, determines whether it can be composed of the digits in the string $num$. If it can be composed, it is added to the answer. The key to the problem is how to determine whether a word can be composed of the digits in the string $num$. We only need to traverse each letter in the word $w$, restore it to the corresponding digit, and compare it with each digit in the string $num$ one by one. If they are the same, it means that the word $w$ can be composed of the digits in the string $num$.</p>
7530775298
<p>The time complexity is $O(m \times n)$, and the space complexity is $O(C)$. Here, $m$ and $n$ are the length of the word list and the string $num$, respectively, and $C$ is the size of the character set, which is $26$ in this problem.</p>
75308-
<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>
75299+
<div class="tabbed-set tabbed-alternate" data-tabs="1:6"><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" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Python3</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C++</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">TypeScript</label></div>
7530975300
<div class="tabbed-content">
7531075301
<div class="tabbed-block">
7531175302
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
@@ -75324,6 +75315,16 @@ <h3 id="solution-1-reverse-thinking">Solution 1: Reverse Thinking</h3>
7532475315
</code></pre></div></td></tr></table></div>
7532575316
</div>
7532675317
<div class="tabbed-block">
75318+
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
75319+
<span class="normal">2</span>
75320+
<span class="normal">3</span>
75321+
<span class="normal">4</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">class</span> <span class="nc">Solution</span><span class="p">:</span>
75322+
<span class="k">def</span> <span class="nf">getValidT9Words</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">words</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
75323+
<span class="n">trans</span> <span class="o">=</span> <span class="nb">str</span><span class="o">.</span><span class="n">maketrans</span><span class="p">(</span><span class="n">ascii_lowercase</span><span class="p">,</span> <span class="s2">&quot;22233344455566677778889999&quot;</span><span class="p">)</span>
75324+
<span class="k">return</span> <span class="p">[</span><span class="n">w</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span> <span class="k">if</span> <span class="n">w</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">trans</span><span class="p">)</span> <span class="o">==</span> <span class="n">num</span><span class="p">]</span>
75325+
</code></pre></div></td></tr></table></div>
75326+
</div>
75327+
<div class="tabbed-block">
7532775328
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
7532875329
<span class="normal"> 2</span>
7532975330
<span class="normal"> 3</span>
@@ -75513,21 +75514,6 @@ <h3 id="solution-1-reverse-thinking">Solution 1: Reverse Thinking</h3>
7551375514
</div>
7551475515
</div>
7551575516
</div>
75516-
<h3 id="solution-2">Solution 2</h3>
75517-
<div class="tabbed-set tabbed-alternate" data-tabs="2:1"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python3</label></div>
75518-
<div class="tabbed-content">
75519-
<div class="tabbed-block">
75520-
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
75521-
<span class="normal">2</span>
75522-
<span class="normal">3</span>
75523-
<span class="normal">4</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">class</span> <span class="nc">Solution</span><span class="p">:</span>
75524-
<span class="k">def</span> <span class="nf">getValidT9Words</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">words</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
75525-
<span class="n">trans</span> <span class="o">=</span> <span class="nb">str</span><span class="o">.</span><span class="n">maketrans</span><span class="p">(</span><span class="n">ascii_lowercase</span><span class="p">,</span> <span class="s2">&quot;22233344455566677778889999&quot;</span><span class="p">)</span>
75526-
<span class="k">return</span> <span class="p">[</span><span class="n">w</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span> <span class="k">if</span> <span class="n">w</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">trans</span><span class="p">)</span> <span class="o">==</span> <span class="n">num</span><span class="p">]</span>
75527-
</code></pre></div></td></tr></table></div>
75528-
</div>
75529-
</div>
75530-
</div>
7553175517
<!-- end -->
7553275518

7553375519

en/lcci/16.22/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75321,10 +75321,10 @@ <h2 id="description">Description</h2>
7532175321

7532275322
<h2 id="solutions">Solutions</h2>
7532375323
<h3 id="solution-1-hash-table-simulation">Solution 1: Hash Table + Simulation</h3>
75324-
<p>We use a hash table $black$ to record the positions of all black squares, and a hash table $dirs$ to record the four directions of the ant. We use variables $x, y$ to record the position of the ant, and variable $p$ to record the direction of the ant. We use variables $x1, y1, x2, y2$ to record the minimum horizontal coordinate, minimum vertical coordinate, maximum horizontal coordinate, and maximum vertical coordinate of all black squares.</p>
75325-
<p>We simulate the ant's walking process. If the square where the ant is located is white, the ant turns right by $90$ degrees, paints the square black, and moves forward one unit. If the square where the ant is located is black, the ant turns left by $90$ degrees, paints the square white, and moves forward one unit. During the simulation, we continuously update the values of $x1, y1, x2, y2$ so that they can contain all the squares the ant has walked through.</p>
75326-
<p>After the simulation is over, we construct the answer matrix $g$ based on the values of $x1, y1, x2, y2$. Then, we paint the direction of the ant on the square where the ant is located, paint all black squares with $X$, and finally return the answer matrix.</p>
75327-
<p>The time complexity is $O(K)$, and the space complexity is $O(K)$. Here, $K$ is the number of steps the ant walks.</p>
75324+
<p>We use a hash table <code>black</code> to record the positions of all black squares, and a hash table <code>dirs</code> to record the four directions of the ant. We use variables $x$, $y$ to record the position of the ant, and variable $p$ to record the direction of the ant. We use variables $x_1$, $y_1$, $x_2$, $y_2$ to record the minimum x-coordinate, minimum y-coordinate, maximum x-coordinate, and maximum y-coordinate of all black squares, respectively.</p>
75325+
<p>We simulate the walking process of the ant. If the square where the ant is located is white, then the ant turns right by $90$ degrees, paints the square black, and moves forward one unit. If the square where the ant is located is black, then the ant turns left by $90$ degrees, paints the square white, and moves forward one unit. During the simulation process, we continuously update the values of $x_1$, $y_1$, $x_2$, $y_2$ so that they can include all the squares that the ant has walked through.</p>
75326+
<p>After the simulation, we construct the answer matrix $g$ based on the values of $x_1$, $y_1$, $x_2$, $y_2$. Then, we paint the direction of the ant at the ant's position, and paint all black squares with $X$, and finally return the answer matrix.</p>
75327+
<p>The time complexity is $O(K)$, and the space complexity is $O(K)$. Where $K$ is the number of steps the ant walks.</p>
7532875328
<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>
7532975329
<div class="tabbed-content">
7533075330
<div class="tabbed-block">

0 commit comments

Comments
 (0)