Skip to content

Commit 3bd6b1d

Browse files
committed
deploy: fc4feaf
1 parent 471df5a commit 3bd6b1d

File tree

20 files changed

+338
-472
lines changed

20 files changed

+338
-472
lines changed

en/lc/2276/index.html

Lines changed: 116 additions & 210 deletions
Large diffs are not rendered by default.

en/lc/2282/index.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57665,9 +57665,9 @@
5766557665
<ul class="md-nav__list">
5766657666

5766757667
<li class="md-nav__item">
57668-
<a href="#solution-1" class="md-nav__link">
57668+
<a href="#solution-1-monotonic-stack" class="md-nav__link">
5766957669
<span class="md-ellipsis">
57670-
Solution 1
57670+
Solution 1: Monotonic Stack
5767157671
</span>
5767257672
</a>
5767357673

@@ -86572,7 +86572,18 @@ <h2 id="description">Description</h2>
8657286572
<h2 id="solutions">Solutions</h2>
8657386573
<!-- solution:start -->
8657486574

86575-
<h3 id="solution-1">Solution 1</h3>
86575+
<h3 id="solution-1-monotonic-stack">Solution 1: Monotonic Stack</h3>
86576+
<p>We observe that for the <span class="arithmatex">\(i\)</span>-th person, the people he can see must have heights that are strictly monotonically increasing from left to right (or from top to bottom).</p>
86577+
<p>Therefore, for each row, we can use a monotonic stack to find the number of people each person can see.</p>
86578+
<p>Specifically, we can traverse the array in reverse order, using a stack <span class="arithmatex">\(stk\)</span> that is monotonically increasing from top to bottom to record the heights of the people we have traversed.</p>
86579+
<p>For the <span class="arithmatex">\(i\)</span>-th person, if the stack is not empty and the top element of the stack is less than <span class="arithmatex">\(heights[i]\)</span>, we increment the number of people the <span class="arithmatex">\(i\)</span>-th person can see, and then pop the top element of the stack, repeating this until the stack is empty or the top element of the stack is greater than or equal to <span class="arithmatex">\(heights[i]\)</span>. If the stack is not empty at this point, it means the top element of the stack is greater than or equal to <span class="arithmatex">\(heights[i]\)</span>, so we increment the number of people the <span class="arithmatex">\(i\)</span>-th person can see by 1. Next, if the stack is not empty and the top element of the stack is equal to <span class="arithmatex">\(heights[i]\)</span>, we pop the top element of the stack. Finally, we push <span class="arithmatex">\(heights[i]\)</span> onto the stack and continue to the next person.</p>
86580+
<p>After processing this way, we can get the number of people each person can see for each row.</p>
86581+
<p>Similarly, we can process each column to get the number of people each person can see for each column. Finally, we add the answers for each row and each column to get the final answer.</p>
86582+
<p>The time complexity is <span class="arithmatex">\(O(m \times n)\)</span>, and the space complexity is <span class="arithmatex">\(O(\max(m, n))\)</span>. Where <span class="arithmatex">\(m\)</span> and <span class="arithmatex">\(n\)</span> are the number of rows and columns of the array <span class="arithmatex">\(heights\)</span>, respectively.</p>
86583+
<p>Similar problems:</p>
86584+
<ul>
86585+
<li><a href="https://github.com/doocs/leetcode/blob/main/solution/1900-1999/1944.Number%20of%20Visible%20People%20in%20a%20Queue/README_EN.md">1944. Number of Visible People in a Queue</a></li>
86586+
</ul>
8657686587
<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>
8657786588
<div class="tabbed-content">
8657886589
<div class="tabbed-block">

en/lc/2287/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57775,9 +57775,9 @@
5777557775
<ul class="md-nav__list">
5777657776

5777757777
<li class="md-nav__item">
57778-
<a href="#solution-1" class="md-nav__link">
57778+
<a href="#solution-1-counting" class="md-nav__link">
5777957779
<span class="md-ellipsis">
57780-
Solution 1
57780+
Solution 1: Counting
5778157781
</span>
5778257782
</a>
5778357783

@@ -86567,7 +86567,9 @@ <h2 id="description">Description</h2>
8656786567
<h2 id="solutions">Solutions</h2>
8656886568
<!-- solution:start -->
8656986569

86570-
<h3 id="solution-1">Solution 1</h3>
86570+
<h3 id="solution-1-counting">Solution 1: Counting</h3>
86571+
<p>We count the occurrences of each character in the strings <span class="arithmatex">\(\textit{s}\)</span> and <span class="arithmatex">\(\textit{target}\)</span>, denoted as <span class="arithmatex">\(\textit{cnt1}\)</span> and <span class="arithmatex">\(\textit{cnt2}\)</span>. For each character in <span class="arithmatex">\(\textit{target}\)</span>, we calculate the number of times it appears in <span class="arithmatex">\(\textit{cnt1}\)</span> divided by the number of times it appears in <span class="arithmatex">\(\textit{cnt2}\)</span>, and take the minimum value.</p>
86572+
<p>The time complexity is <span class="arithmatex">\(O(n + m)\)</span>, and the space complexity is <span class="arithmatex">\(O(|\Sigma|)\)</span>. Where <span class="arithmatex">\(n\)</span> and <span class="arithmatex">\(m\)</span> are the lengths of the strings <span class="arithmatex">\(\textit{s}\)</span> and <span class="arithmatex">\(\textit{target}\)</span>, respectively. And <span class="arithmatex">\(|\Sigma|\)</span> is the size of the character set, which is 26 in this problem.</p>
8657186573
<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>
8657286574
<div class="tabbed-content">
8657386575
<div class="tabbed-block">

en/lc/2290/index.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57841,9 +57841,9 @@
5784157841
<ul class="md-nav__list">
5784257842

5784357843
<li class="md-nav__item">
57844-
<a href="#solution-1" class="md-nav__link">
57844+
<a href="#solution-1-double-ended-queue-bfs" class="md-nav__link">
5784557845
<span class="md-ellipsis">
57846-
Solution 1
57846+
Solution 1: Double-Ended Queue BFS
5784757847
</span>
5784857848
</a>
5784957849

@@ -86577,7 +86577,17 @@ <h2 id="description">Description</h2>
8657786577
<h2 id="solutions">Solutions</h2>
8657886578
<!-- solution:start -->
8657986579

86580-
<h3 id="solution-1">Solution 1</h3>
86580+
<h3 id="solution-1-double-ended-queue-bfs">Solution 1: Double-Ended Queue BFS</h3>
86581+
<p>This problem is essentially a shortest path model, but we need to find the minimum number of obstacles to remove.</p>
86582+
<p>In an undirected graph with edge weights of only <span class="arithmatex">\(0\)</span> and <span class="arithmatex">\(1\)</span>, we can use a double-ended queue to perform BFS. The principle is that if the weight of the current point that can be expanded is <span class="arithmatex">\(0\)</span>, it is added to the front of the queue; if the weight is <span class="arithmatex">\(1\)</span>, it is added to the back of the queue.</p>
86583+
<blockquote>
86584+
<p>If the weight of an edge is <span class="arithmatex">\(0\)</span>, then the newly expanded node has the same weight as the current front node, and it can obviously be used as the starting point for the next expansion.</p>
86585+
</blockquote>
86586+
<p>The time complexity is <span class="arithmatex">\(O(m \times n)\)</span>, and the space complexity is <span class="arithmatex">\(O(m \times n)\)</span>. Where <span class="arithmatex">\(m\)</span> and <span class="arithmatex">\(n\)</span> are the number of rows and columns of the grid, respectively.</p>
86587+
<p>Similar problems:</p>
86588+
<ul>
86589+
<li><a href="https://github.com/doocs/leetcode/blob/main/solution/1300-1399/1368.Minimum%20Cost%20to%20Make%20at%20Least%20One%20Valid%20Path%20in%20a%20Grid/README_EN.md">1368. Minimum Cost to Make at Least One Valid Path in a Grid</a></li>
86590+
</ul>
8658186591
<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>
8658286592
<div class="tabbed-content">
8658386593
<div class="tabbed-block">

en/lc/2291/index.html

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57863,18 +57863,18 @@
5786357863
<ul class="md-nav__list">
5786457864

5786557865
<li class="md-nav__item">
57866-
<a href="#solution-1" class="md-nav__link">
57866+
<a href="#solution-1-dynamic-programming" class="md-nav__link">
5786757867
<span class="md-ellipsis">
57868-
Solution 1
57868+
Solution 1: Dynamic Programming
5786957869
</span>
5787057870
</a>
5787157871

5787257872
</li>
5787357873

5787457874
<li class="md-nav__item">
57875-
<a href="#solution-2" class="md-nav__link">
57875+
<a href="#solution-2-dynamic-programming-space-optimization" class="md-nav__link">
5787657876
<span class="md-ellipsis">
57877-
Solution 2
57877+
Solution 2: Dynamic Programming (Space Optimization)
5787857878
</span>
5787957879
</a>
5788057880

@@ -86567,7 +86567,15 @@ <h2 id="description">Description</h2>
8656786567
<h2 id="solutions">Solutions</h2>
8656886568
<!-- solution:start -->
8656986569

86570-
<h3 id="solution-1">Solution 1</h3>
86570+
<h3 id="solution-1-dynamic-programming">Solution 1: Dynamic Programming</h3>
86571+
<p>We define <span class="arithmatex">\(f[i][j]\)</span> to represent the maximum profit when considering the first <span class="arithmatex">\(i\)</span> stocks with a budget of <span class="arithmatex">\(j\)</span>. The answer is <span class="arithmatex">\(f[n][\textit{budget}]\)</span>.</p>
86572+
<p>For the <span class="arithmatex">\(i\)</span>-th stock, we have two choices:</p>
86573+
<ul>
86574+
<li>Do not buy it, then <span class="arithmatex">\(f[i][j] = f[i - 1][j]\)</span>;</li>
86575+
<li>Buy it, then <span class="arithmatex">\(f[i][j] = f[i - 1][j - \textit{present}[i]] + \textit{future}[i] - \textit{present}[i]\)</span>.</li>
86576+
</ul>
86577+
<p>Finally, return <span class="arithmatex">\(f[n][\textit{budget}]\)</span>.</p>
86578+
<p>The time complexity is <span class="arithmatex">\(O(n \times \textit{budget})\)</span>, and the space complexity is <span class="arithmatex">\(O(n \times \textit{budget})\)</span>. Where <span class="arithmatex">\(n\)</span> is the length of the array.</p>
8657186579
<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>
8657286580
<div class="tabbed-content">
8657386581
<div class="tabbed-block">
@@ -86722,7 +86730,8 @@ <h3 id="solution-1">Solution 1</h3>
8672286730

8672386731
<!-- solution:start -->
8672486732

86725-
<h3 id="solution-2">Solution 2</h3>
86733+
<h3 id="solution-2-dynamic-programming-space-optimization">Solution 2: Dynamic Programming (Space Optimization)</h3>
86734+
<p>We can observe that for each row, we only need the values from the previous row, so we can optimize the space complexity to <span class="arithmatex">\(O(\text{budget})\)</span>.</p>
8672686735
<div class="tabbed-set tabbed-alternate" data-tabs="2:4"><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" /><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></div>
8672786736
<div class="tabbed-content">
8672886737
<div class="tabbed-block">

en/lc/2311/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58303,9 +58303,9 @@
5830358303
<ul class="md-nav__list">
5830458304

5830558305
<li class="md-nav__item">
58306-
<a href="#solution-1" class="md-nav__link">
58306+
<a href="#solution-1-greedy" class="md-nav__link">
5830758307
<span class="md-ellipsis">
58308-
Solution 1
58308+
Solution 1: Greedy
5830958309
</span>
5831058310
</a>
5831158311

@@ -86564,7 +86564,9 @@ <h2 id="description">Description</h2>
8656486564
<h2 id="solutions">Solutions</h2>
8656586565
<!-- solution:start -->
8656686566

86567-
<h3 id="solution-1">Solution 1</h3>
86567+
<h3 id="solution-1-greedy">Solution 1: Greedy</h3>
86568+
<p>The longest binary subsequence must include all the <span class="arithmatex">\(0\)</span>s in the original string. On this basis, we traverse <span class="arithmatex">\(s\)</span> from right to left. If we encounter a <span class="arithmatex">\(1\)</span>, we check if adding this <span class="arithmatex">\(1\)</span> to the subsequence keeps the binary number <span class="arithmatex">\(v \leq k\)</span>.</p>
86569+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, where <span class="arithmatex">\(n\)</span> is the length of the string <span class="arithmatex">\(s\)</span>. The space complexity is <span class="arithmatex">\(O(1)\)</span>.</p>
8656886570
<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">JavaScript</label><label for="__tabbed_1_7">C#</label></div>
8656986571
<div class="tabbed-content">
8657086572
<div class="tabbed-block">

en/lc/2313/index.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58347,9 +58347,9 @@
5834758347
<ul class="md-nav__list">
5834858348

5834958349
<li class="md-nav__item">
58350-
<a href="#solution-1" class="md-nav__link">
58350+
<a href="#solution-1-tree-dp-case-analysis" class="md-nav__link">
5835158351
<span class="md-ellipsis">
58352-
Solution 1
58352+
Solution 1: Tree DP + Case Analysis
5835358353
</span>
5835458354
</a>
5835558355

@@ -86579,7 +86579,19 @@ <h2 id="description">Description</h2>
8657986579
<h2 id="solutions">Solutions</h2>
8658086580
<!-- solution:start -->
8658186581

86582-
<h3 id="solution-1">Solution 1</h3>
86582+
<h3 id="solution-1-tree-dp-case-analysis">Solution 1: Tree DP + Case Analysis</h3>
86583+
<p>We define a function <span class="arithmatex">\(dfs(root)\)</span>, which returns an array of length 2. The first element represents the minimum number of flips needed to change the value of the <span class="arithmatex">\(root\)</span> node to <code>false</code>, and the second element represents the minimum number of flips needed to change the value of the <span class="arithmatex">\(root\)</span> node to <code>true</code>. The answer is <span class="arithmatex">\(dfs(root)[result]\)</span>.</p>
86584+
<p>The implementation of the function <span class="arithmatex">\(dfs(root)\)</span> is as follows:</p>
86585+
<p>If <span class="arithmatex">\(root\)</span> is null, return <span class="arithmatex">\([+\infty, +\infty]\)</span>.</p>
86586+
<p>Otherwise, let <span class="arithmatex">\(x\)</span> be the value of <span class="arithmatex">\(root\)</span>, <span class="arithmatex">\(l\)</span> be the return value of the left subtree, and <span class="arithmatex">\(r\)</span> be the return value of the right subtree. Then we discuss the following cases:</p>
86587+
<ul>
86588+
<li>If <span class="arithmatex">\(x \in \{0, 1\}\)</span>, return <span class="arithmatex">\([x, x \oplus 1]\)</span>.</li>
86589+
<li>If <span class="arithmatex">\(x = 2\)</span>, which means the boolean operator is <code>OR</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need to make both the left and right subtrees <code>false</code>. Therefore, the first element of the return value is <span class="arithmatex">\(l[0] + r[0]\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need at least one of the left or right subtrees to be <code>true</code>. Therefore, the second element of the return value is <span class="arithmatex">\(\min(l[0] + r[1], l[1] + r[0], l[1] + r[1])\)</span>.</li>
86590+
<li>If <span class="arithmatex">\(x = 3\)</span>, which means the boolean operator is <code>AND</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need at least one of the left or right subtrees to be <code>false</code>. Therefore, the first element of the return value is <span class="arithmatex">\(\min(l[0] + r[0], l[0] + r[1], l[1] + r[0])\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need both the left and right subtrees to be <code>true</code>. Therefore, the second element of the return value is <span class="arithmatex">\(l[1] + r[1]\)</span>.</li>
86591+
<li>If <span class="arithmatex">\(x = 4\)</span>, which means the boolean operator is <code>XOR</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need both the left and right subtrees to be either <code>false</code> or <code>true</code>. Therefore, the first element of the return value is <span class="arithmatex">\(\min(l[0] + r[0], l[1] + r[1])\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need the left and right subtrees to be different. Therefore, the second element of the return value is <span class="arithmatex">\(\min(l[0] + r[1], l[1] + r[0])\)</span>.</li>
86592+
<li>If <span class="arithmatex">\(x = 5\)</span>, which means the boolean operator is <code>NOT</code>, to make the value of <span class="arithmatex">\(root\)</span> <code>false</code>, we need at least one of the left or right subtrees to be <code>true</code>. Therefore, the first element of the return value is <span class="arithmatex">\(\min(l[1], r[1])\)</span>. To make the value of <span class="arithmatex">\(root\)</span> <code>true</code>, we need at least one of the left or right subtrees to be <code>false</code>. Therefore, the second element of the return value is <span class="arithmatex">\(\min(l[0], r[0])\)</span>.</li>
86593+
</ul>
86594+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Where <span class="arithmatex">\(n\)</span> is the number of nodes in the binary tree.</p>
8658386595
<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>
8658486596
<div class="tabbed-content">
8658586597
<div class="tabbed-block">

0 commit comments

Comments
 (0)