Skip to content

Commit b60e73d

Browse files
committed
deploy: 3b44422
1 parent 63d0cae commit b60e73d

File tree

10 files changed

+7365
-7139
lines changed

10 files changed

+7365
-7139
lines changed

en/lc/1461/index.html

Lines changed: 188 additions & 79 deletions
Large diffs are not rendered by default.

en/lc/1462/index.html

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37704,18 +37704,18 @@
3770437704
<ul class="md-nav__list">
3770537705

3770637706
<li class="md-nav__item">
37707-
<a href="#solution-1" class="md-nav__link">
37707+
<a href="#solution-1-floyds-algorithm" class="md-nav__link">
3770837708
<span class="md-ellipsis">
37709-
Solution 1
37709+
Solution 1: Floyd's Algorithm
3771037710
</span>
3771137711
</a>
3771237712

3771337713
</li>
3771437714

3771537715
<li class="md-nav__item">
37716-
<a href="#solution-2" class="md-nav__link">
37716+
<a href="#solution-2-topological-sorting" class="md-nav__link">
3771737717
<span class="md-ellipsis">
37718-
Solution 2
37718+
Solution 2: Topological Sorting
3771937719
</span>
3772037720
</a>
3772137721

@@ -77739,7 +77739,13 @@ <h2 id="description">Description</h2>
7773977739
<h2 id="solutions">Solutions</h2>
7774077740
<!-- solution:start -->
7774177741

77742-
<h3 id="solution-1">Solution 1</h3>
77742+
<h3 id="solution-1-floyds-algorithm">Solution 1: Floyd's Algorithm</h3>
77743+
<p>We create a 2D array $f$, where $f[i][j]$ indicates whether node $i$ can reach node $j$.</p>
77744+
<p>Next, we iterate through the prerequisites array $prerequisites$. For each item $[a, b]$ in it, we set $f[a][b]$ to $true$.</p>
77745+
<p>Then, we use Floyd's algorithm to compute the reachability between all pairs of nodes.</p>
77746+
<p>Specifically, we use three nested loops: first enumerating the intermediate node $k$, then the starting node $i$, and finally the ending node $j$. For each iteration, if node $i$ can reach node $k$ and node $k$ can reach node $j$, then node $i$ can also reach node $j$, and we set $f[i][j]$ to $true$.</p>
77747+
<p>After computing the reachability between all pairs of nodes, for each query $[a, b]$, we can directly return $f[a][b]$.</p>
77748+
<p>The time complexity is $O(n^3)$, and the space complexity is $O(n^2)$, where $n$ is the number of nodes.</p>
7774377749
<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>
7774477750
<div class="tabbed-content">
7774577751
<div class="tabbed-block">
@@ -77932,7 +77938,13 @@ <h3 id="solution-1">Solution 1</h3>
7793277938

7793377939
<!-- solution:start -->
7793477940

77935-
<h3 id="solution-2">Solution 2</h3>
77941+
<h3 id="solution-2-topological-sorting">Solution 2: Topological Sorting</h3>
77942+
<p>Similar to Solution 1, we create a 2D array $f$, where $f[i][j]$ indicates whether node $i$ can reach node $j$. Additionally, we create an adjacency list $g$, where $g[i]$ represents all successor nodes of node $i$, and an array $indeg$, where $indeg[i]$ represents the in-degree of node $i$.</p>
77943+
<p>Next, we iterate through the prerequisites array $prerequisites$. For each item $[a, b]$ in it, we update the adjacency list $g$ and the in-degree array $indeg$.</p>
77944+
<p>Then, we use topological sorting to compute the reachability between all pairs of nodes.</p>
77945+
<p>We define a queue $q$, initially adding all nodes with an in-degree of $0$ to the queue. Then, we continuously perform the following operations: remove the front node $i$ from the queue, then iterate through all nodes $j$ in $g[i]$, setting $f[i][j]$ to $true$. Next, we enumerate node $h$, and if $f[h][i]$ is $true$, we also set $f[h][j]$ to $true$. After this, we decrease the in-degree of $j$ by $1$. If the in-degree of $j$ becomes $0$, we add $j$ to the queue.</p>
77946+
<p>After computing the reachability between all pairs of nodes, for each query $[a, b]$, we can directly return $f[a][b]$.</p>
77947+
<p>The time complexity is $O(n^3)$, and the space complexity is $O(n^2)$, where $n$ is the number of nodes.</p>
7793677948
<div class="tabbed-set tabbed-alternate" data-tabs="2:5"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python3</label><label for="__tabbed_2_2">Java</label><label for="__tabbed_2_3">C++</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">TypeScript</label></div>
7793777949
<div class="tabbed-content">
7793877950
<div class="tabbed-block">

en/search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)