Skip to content

Commit 192de93

Browse files
committed
deploy: ccf55f2
1 parent bb6dedc commit 192de93

File tree

12 files changed

+403
-892
lines changed

12 files changed

+403
-892
lines changed

en/lc/1471/index.html

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38045,9 +38045,9 @@
3804538045
<ul class="md-nav__list">
3804638046

3804738047
<li class="md-nav__item">
38048-
<a href="#solution-1" class="md-nav__link">
38048+
<a href="#solution-1-sorting" class="md-nav__link">
3804938049
<span class="md-ellipsis">
38050-
Solution 1
38050+
Solution 1: Sorting
3805138051
</span>
3805238052
</a>
3805338053

@@ -79469,8 +79469,11 @@ <h2 id="description">Description</h2>
7946979469
<h2 id="solutions">Solutions</h2>
7947079470
<!-- solution:start -->
7947179471

79472-
<h3 id="solution-1">Solution 1</h3>
79473-
<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>
79472+
<h3 id="solution-1-sorting">Solution 1: Sorting</h3>
79473+
<p>We first sort the array $\textit{arr}$ and then find the median $m$ of the array.</p>
79474+
<p>Next, we sort the array according to the rules described in the problem, and finally return the first $k$ elements of the array.</p>
79475+
<p>The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$.</p>
79476+
<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>
7947479477
<div class="tabbed-content">
7947579478
<div class="tabbed-block">
7947679479
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
@@ -79596,6 +79599,18 @@ <h3 id="solution-1">Solution 1</h3>
7959679599
<span class="p">}</span>
7959779600
</code></pre></div></td></tr></table></div>
7959879601
</div>
79602+
<div class="tabbed-block">
79603+
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
79604+
<span class="normal">2</span>
79605+
<span class="normal">3</span>
79606+
<span class="normal">4</span>
79607+
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="kd">function</span><span class="w"> </span><span class="nx">getStrongest</span><span class="p">(</span><span class="nx">arr</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[],</span><span class="w"> </span><span class="nx">k</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>
79608+
<span class="w"> </span><span class="nx">arr</span><span class="p">.</span><span class="nx">sort</span><span class="p">((</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">b</span><span class="p">);</span>
79609+
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">arr</span><span class="p">[(</span><span class="nx">arr</span><span class="p">.</span><span class="nx">length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mf">1</span><span class="p">];</span>
79610+
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">arr</span><span class="p">.</span><span class="nx">sort</span><span class="p">((</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">abs</span><span class="p">(</span><span class="nx">b</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">m</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">abs</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">m</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">a</span><span class="p">).</span><span class="nx">slice</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">k</span><span class="p">);</span>
79611+
<span class="p">}</span>
79612+
</code></pre></div></td></tr></table></div>
79613+
</div>
7959979614
</div>
7960079615
</div>
7960179616
<!-- solution:end -->

en/lc/1472/index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38066,9 +38066,9 @@
3806638066
<ul class="md-nav__list">
3806738067

3806838068
<li class="md-nav__item">
38069-
<a href="#solution-1" class="md-nav__link">
38069+
<a href="#solution-1-two-stacks" class="md-nav__link">
3807038070
<span class="md-ellipsis">
38071-
Solution 1
38071+
Solution 1: Two Stacks
3807238072
</span>
3807338073
</a>
3807438074

@@ -79483,7 +79483,12 @@ <h2 id="description">Description</h2>
7948379483
<h2 id="solutions">Solutions</h2>
7948479484
<!-- solution:start -->
7948579485

79486-
<h3 id="solution-1">Solution 1</h3>
79486+
<h3 id="solution-1-two-stacks">Solution 1: Two Stacks</h3>
79487+
<p>We can use two stacks, $\textit{stk1}$ and $\textit{stk2}$, to store the back and forward pages, respectively. Initially, $\textit{stk1}$ contains the $\textit{homepage}$, and $\textit{stk2}$ is empty.</p>
79488+
<p>When calling $\text{visit}(url)$, we add $\textit{url}$ to $\textit{stk1}$ and clear $\textit{stk2}$. The time complexity is $O(1)$.</p>
79489+
<p>When calling $\text{back}(steps)$, we pop the top element from $\textit{stk1}$ and push it to $\textit{stk2}$. We repeat this operation $steps$ times until the length of $\textit{stk1}$ is $1$ or $steps$ is $0$. Finally, we return the top element of $\textit{stk1}$. The time complexity is $O(\textit{steps})$.</p>
79490+
<p>When calling $\text{forward}(steps)$, we pop the top element from $\textit{stk2}$ and push it to $\textit{stk1}$. We repeat this operation $steps$ times until $\textit{stk2}$ is empty or $steps$ is $0$. Finally, we return the top element of $\textit{stk1}$. The time complexity is $O(\textit{steps})$.</p>
79491+
<p>The space complexity is $O(n)$, where $n$ is the length of the browsing history.</p>
7948779492
<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>
7948879493
<div class="tabbed-content">
7948979494
<div class="tabbed-block">

en/lc/1473/index.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38087,9 +38087,9 @@
3808738087
<ul class="md-nav__list">
3808838088

3808938089
<li class="md-nav__item">
38090-
<a href="#solution-1" class="md-nav__link">
38090+
<a href="#solution-1-dynamic-programming" class="md-nav__link">
3809138091
<span class="md-ellipsis">
38092-
Solution 1
38092+
Solution 1: Dynamic Programming
3809338093
</span>
3809438094
</a>
3809538095

@@ -79442,7 +79442,7 @@ <h2 id="description">Description</h2>
7944279442
<strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
7944379443
<strong>Output:</strong> 11
7944479444
<strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2]
79445-
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
79445+
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
7944679446
Cost of paint the first and last house (10 + 1) = 11.
7944779447
</pre>
7944879448

@@ -79472,7 +79472,19 @@ <h2 id="description">Description</h2>
7947279472
<h2 id="solutions">Solutions</h2>
7947379473
<!-- solution:start -->
7947479474

79475-
<h3 id="solution-1">Solution 1</h3>
79475+
<h3 id="solution-1-dynamic-programming">Solution 1: Dynamic Programming</h3>
79476+
<p>We define $f[i][j][k]$ to represent the minimum cost to paint houses from index $0$ to $i$, with the last house painted in color $j$, and exactly forming $k$ blocks. The answer is $f[m-1][j][\textit{target}]$, where $j$ ranges from $1$ to $n$. Initially, we check if the house at index $0$ is already painted. If it is not painted, then $f[0][j][1] = \textit{cost}[0][j - 1]$, where $j \in [1,..n]$. If it is already painted, then $f[0][\textit{houses}[0]][1] = 0$. All other values of $f[i][j][k]$ are initialized to $\infty$.</p>
79477+
<p>Next, we start iterating from index $i=1$. For each $i$, we check if the house at index $i$ is already painted:</p>
79478+
<p>If it is not painted, we can paint the house at index $i$ with color $j$. We enumerate the number of blocks $k$, where $k \in [1,..\min(\textit{target}, i + 1)]$, and enumerate the color of the previous house $j_0$, where $j_0 \in [1,..n]$. Then we can derive the state transition equation:</p>
79479+
<p>$$
79480+
f[i][j][k] = \min_{j_0 \in [1,..n]} { f[i - 1][j_0][k - (j \neq j_0)] + \textit{cost}[i][j - 1] }
79481+
$$</p>
79482+
<p>If it is already painted, we can paint the house at index $i$ with color $j$. We enumerate the number of blocks $k$, where $k \in [1,..\min(\textit{target}, i + 1)]$, and enumerate the color of the previous house $j_0$, where $j_0 \in [1,..n]$. Then we can derive the state transition equation:</p>
79483+
<p>$$
79484+
f[i][j][k] = \min_{j_0 \in [1,..n]} { f[i - 1][j_0][k - (j \neq j_0)] }
79485+
$$</p>
79486+
<p>Finally, we return $f[m - 1][j][\textit{target}]$, where $j \in [1,..n]$. If all values of $f[m - 1][j][\textit{target}]$ are $\infty$, then return $-1$.</p>
79487+
<p>The time complexity is $O(m \times n^2 \times \textit{target})$, and the space complexity is $O(m \times n \times \textit{target})$. Here, $m$, $n$, and $\textit{target}$ represent the number of houses, the number of colors, and the number of blocks, respectively.</p>
7947679488
<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>
7947779489
<div class="tabbed-content">
7947879490
<div class="tabbed-block">

0 commit comments

Comments
 (0)