Skip to content

Commit c0dc99b

Browse files
committed
feat: add solutions to lc problems: No.2441~2444
* No.2441.Largest Positive Integer That Exists With Its Negative * No.2442.Count Number of Distinct Integers After Reverse Operations * No.2443.Sum of Number and Its Reverse * No.2444.Count Subarrays With Fixed Bounds
1 parent 2c5797f commit c0dc99b

File tree

74 files changed

+2207
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2207
-142
lines changed

solution/0000-0099/0016.3Sum Closest/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<p><strong>Constraints:</strong></p>
3232

3333
<ul>
34-
<li><code>3 &lt;= nums.length &lt;= 1000</code></li>
34+
<li><code>3 &lt;= nums.length &lt;= 500</code></li>
3535
<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
3636
<li><code>-10<sup>4</sup> &lt;= target &lt;= 10<sup>4</sup></code></li>
3737
</ul>

solution/0000-0099/0039.Combination Sum/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<p>The test cases are generated such that the number of unique combinations that sum up to <code>target</code> is less than <code>150</code> combinations for the given input.</p>
1212

1313
<p>&nbsp;</p>
14-
<p><strong class="example">Example 1:</strong></p>
14+
<p><strong>Example 1:</strong></p>
1515

1616
<pre>
1717
<strong>Input:</strong> candidates = [2,3,6,7], target = 7
@@ -22,14 +22,14 @@
2222
These are the only two combinations.
2323
</pre>
2424

25-
<p><strong class="example">Example 2:</strong></p>
25+
<p><strong>Example 2:</strong></p>
2626

2727
<pre>
2828
<strong>Input:</strong> candidates = [2,3,5], target = 8
2929
<strong>Output:</strong> [[2,2,2,2],[2,3,3],[3,5]]
3030
</pre>
3131

32-
<p><strong class="example">Example 3:</strong></p>
32+
<p><strong>Example 3:</strong></p>
3333

3434
<pre>
3535
<strong>Input:</strong> candidates = [2], target = 1
@@ -43,7 +43,7 @@ These are the only two combinations.
4343
<li><code>1 &lt;= candidates.length &lt;= 30</code></li>
4444
<li><code>2 &lt;= candidates[i] &lt;= 40</code></li>
4545
<li>All elements of <code>candidates</code> are <strong>distinct</strong>.</li>
46-
<li><code>1 &lt;= target &lt;= 500</code></li>
46+
<li><code>1 &lt;= target &lt;= 40</code></li>
4747
</ul>
4848

4949
## Solutions

solution/0100-0199/0192.Word Frequency/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<li>Words are separated by one or more whitespace characters.</li>
1515
</ul>
1616

17-
<p><strong>Example:</strong></p>
17+
<p><strong class="example">Example:</strong></p>
1818

1919
<p>Assume that <code>words.txt</code> has the following content:</p>
2020

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).&rdquo;</p>
1010

1111
<p>&nbsp;</p>
12-
<p><strong class="example">Example 1:</strong></p>
12+
<p><strong>Example 1:</strong></p>
1313
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/images/binarytree.png" style="width: 200px; height: 190px;" />
1414
<pre>
1515
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
1616
<strong>Output:</strong> 3
1717
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
1818
</pre>
1919

20-
<p><strong class="example">Example 2:</strong></p>
20+
<p><strong>Example 2:</strong></p>
2121
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/images/binarytree.png" style="width: 200px; height: 190px;" />
2222
<pre>
2323
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
2424
<strong>Output:</strong> 5
2525
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
2626
</pre>
2727

28-
<p><strong class="example">Example 3:</strong></p>
28+
<p><strong>Example 3:</strong></p>
2929

3030
<pre>
3131
<strong>Input:</strong> root = [1,2], p = 1, q = 2

solution/0300-0399/0306.Additive Number/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<p><strong>Note:</strong> Numbers in the additive sequence <strong>cannot</strong> have leading zeros, so sequence <code>1, 2, 03</code> or <code>1, 02, 3</code> is invalid.</p>
1414

1515
<p>&nbsp;</p>
16-
<p><strong class="example">Example 1:</strong></p>
16+
<p><strong>Example 1:</strong></p>
1717

1818
<pre>
1919
<strong>Input:</strong> &quot;112358&quot;
@@ -23,7 +23,7 @@ The digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
2323
1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
2424
</pre>
2525

26-
<p><strong class="example">Example 2:</strong></p>
26+
<p><strong>Example 2:</strong></p>
2727

2828
<pre>
2929
<strong>Input:</strong> &quot;199100199&quot;

solution/0300-0399/0320.Generalized Abbreviation/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
<p>Given a string <code>word</code>, return <em>a list of all the possible <strong>generalized abbreviations</strong> of</em> <code>word</code>. Return the answer in <strong>any order</strong>.</p>
2727

2828
<p>&nbsp;</p>
29-
<p><strong>Example 1:</strong></p>
29+
<p><strong class="example">Example 1:</strong></p>
3030
<pre><strong>Input:</strong> word = "word"
3131
<strong>Output:</strong> ["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"]
32-
</pre><p><strong>Example 2:</strong></p>
32+
</pre><p><strong class="example">Example 2:</strong></p>
3333
<pre><strong>Input:</strong> word = "a"
3434
<strong>Output:</strong> ["1","a"]
3535
</pre>

solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p>
88

99
<p>&nbsp;</p>
10-
<p><strong class="example">Example 1:</strong></p>
10+
<p><strong>Example 1:</strong></p>
1111

1212
<pre>
1313
<strong>Input:</strong> nums = [1,2,3,4,5]
1414
<strong>Output:</strong> true
1515
<strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid.
1616
</pre>
1717

18-
<p><strong class="example">Example 2:</strong></p>
18+
<p><strong>Example 2:</strong></p>
1919

2020
<pre>
2121
<strong>Input:</strong> nums = [5,4,3,2,1]
2222
<strong>Output:</strong> false
2323
<strong>Explanation:</strong> No triplet exists.
2424
</pre>
2525

26-
<p><strong class="example">Example 3:</strong></p>
26+
<p><strong>Example 3:</strong></p>
2727

2828
<pre>
2929
<strong>Input:</strong> nums = [2,1,5,0,4,6]

solution/0600-0699/0679.24 Game/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
<p>Return <code>true</code> if you can get such expression that evaluates to <code>24</code>, and <code>false</code> otherwise.</p>
3030

3131
<p>&nbsp;</p>
32-
<p><strong>Example 1:</strong></p>
32+
<p><strong class="example">Example 1:</strong></p>
3333

3434
<pre>
3535
<strong>Input:</strong> cards = [4,1,8,7]
3636
<strong>Output:</strong> true
3737
<strong>Explanation:</strong> (8-4) * (7-1) = 24
3838
</pre>
3939

40-
<p><strong>Example 2:</strong></p>
40+
<p><strong class="example">Example 2:</strong></p>
4141

4242
<pre>
4343
<strong>Input:</strong> cards = [1,2,1,2]

solution/0600-0699/0682.Baseball Game/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<p>The test cases are generated such that the answer and all intermediate calculations fit in a <strong>32-bit</strong> integer and that all operations are valid.</p>
3737

3838
<p>&nbsp;</p>
39-
<p><strong>Example 1:</strong></p>
39+
<p><strong class="example">Example 1:</strong></p>
4040

4141
<pre>
4242
<strong>Input:</strong> ops = [&quot;5&quot;,&quot;2&quot;,&quot;C&quot;,&quot;D&quot;,&quot;+&quot;]
@@ -50,7 +50,7 @@
5050
The total sum is 5 + 10 + 15 = 30.
5151
</pre>
5252

53-
<p><strong>Example 2:</strong></p>
53+
<p><strong class="example">Example 2:</strong></p>
5454

5555
<pre>
5656
<strong>Input:</strong> ops = [&quot;5&quot;,&quot;-2&quot;,&quot;4&quot;,&quot;C&quot;,&quot;D&quot;,&quot;9&quot;,&quot;+&quot;,&quot;+&quot;]
@@ -67,7 +67,7 @@ The total sum is 5 + 10 + 15 = 30.
6767
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
6868
</pre>
6969

70-
<p><strong>Example 3:</strong></p>
70+
<p><strong class="example">Example 3:</strong></p>
7171

7272
<pre>
7373
<strong>Input:</strong> ops = [&quot;1&quot;,&quot;C&quot;]

solution/0700-0799/0724.Find Pivot Index/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
<p>If the index is on the left edge of the array, then the left sum is <code>0</code> because there are no elements to the left. This also applies to the right edge of the array.</p>
1212

13-
<p>Return <em>the <strong>leftmost pivot index</strong></em>. If no such index exists, return -1.</p>
13+
<p>Return <em>the <strong>leftmost pivot index</strong></em>. If no such index exists, return <code>-1</code>.</p>
1414

1515
<p>&nbsp;</p>
16-
<p><strong class="example">Example 1:</strong></p>
16+
<p><strong>Example 1:</strong></p>
1717

1818
<pre>
1919
<strong>Input:</strong> nums = [1,7,3,6,5,6]
@@ -24,15 +24,15 @@ Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
2424
Right sum = nums[4] + nums[5] = 5 + 6 = 11
2525
</pre>
2626

27-
<p><strong class="example">Example 2:</strong></p>
27+
<p><strong>Example 2:</strong></p>
2828

2929
<pre>
3030
<strong>Input:</strong> nums = [1,2,3]
3131
<strong>Output:</strong> -1
3232
<strong>Explanation:</strong>
3333
There is no index that satisfies the conditions in the problem statement.</pre>
3434

35-
<p><strong class="example">Example 3:</strong></p>
35+
<p><strong>Example 3:</strong></p>
3636

3737
<pre>
3838
<strong>Input:</strong> nums = [2,1,-1]

solution/0700-0799/0732.My Calendar III/README_EN.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<p>A <code>k</code>-booking happens when <code>k</code> events have some non-empty intersection (i.e., there is some time that is common to all <code>k</code> events.)</p>
88

9-
<p>You are given some events <code>[start, end)</code>, after each given event, return an integer <code>k</code> representing the maximum <code>k</code>-booking between all the previous events.</p>
9+
<p>You are given some events <code>[startTime, endTime)</code>, after each given event, return an integer <code>k</code> representing the maximum <code>k</code>-booking between all the previous events.</p>
1010

1111
<p>Implement the <code>MyCalendarThree</code> class:</p>
1212

1313
<ul>
1414
<li><code>MyCalendarThree()</code> Initializes the object.</li>
15-
<li><code>int book(int start, int end)</code> Returns an integer <code>k</code> representing the largest integer such that there exists a <code>k</code>-booking in the calendar.</li>
15+
<li><code>int book(int startTime, int endTime)</code> Returns an integer <code>k</code> representing the largest integer such that there exists a <code>k</code>-booking in the calendar.</li>
1616
</ul>
1717

1818
<p>&nbsp;</p>
@@ -27,19 +27,20 @@
2727

2828
<strong>Explanation</strong>
2929
MyCalendarThree myCalendarThree = new MyCalendarThree();
30-
myCalendarThree.book(10, 20); // return 1, The first event can be booked and is disjoint, so the maximum k-booking is a 1-booking.
31-
myCalendarThree.book(50, 60); // return 1, The second event can be booked and is disjoint, so the maximum k-booking is a 1-booking.
32-
myCalendarThree.book(10, 40); // return 2, The third event [10, 40) intersects the first event, and the maximum k-booking is a 2-booking.
33-
myCalendarThree.book(5, 15); // return 3, The remaining events cause the maximum K-booking to be only a 3-booking.
30+
myCalendarThree.book(10, 20); // return 1
31+
myCalendarThree.book(50, 60); // return 1
32+
myCalendarThree.book(10, 40); // return 2
33+
myCalendarThree.book(5, 15); // return 3
3434
myCalendarThree.book(5, 10); // return 3
3535
myCalendarThree.book(25, 55); // return 3
36+
3637
</pre>
3738

3839
<p>&nbsp;</p>
3940
<p><strong>Constraints:</strong></p>
4041

4142
<ul>
42-
<li><code>0 &lt;= start &lt; end &lt;= 10<sup>9</sup></code></li>
43+
<li><code>0 &lt;= startTime &lt; endTime &lt;= 10<sup>9</sup></code></li>
4344
<li>At most <code>400</code> calls will be made to <code>book</code>.</li>
4445
</ul>
4546

solution/0700-0799/0739.Daily Temperatures/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<p>Given an array of integers <code>temperatures</code> represents the daily temperatures, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is the number of days you have to wait after the</em> <code>i<sup>th</sup></code> <em>day to get a warmer temperature</em>. If there is no future day for which this is possible, keep <code>answer[i] == 0</code> instead.</p>
88

99
<p>&nbsp;</p>
10-
<p><strong class="example">Example 1:</strong></p>
10+
<p><strong>Example 1:</strong></p>
1111
<pre><strong>Input:</strong> temperatures = [73,74,75,71,69,72,76,73]
1212
<strong>Output:</strong> [1,1,4,2,1,1,0,0]
13-
</pre><p><strong class="example">Example 2:</strong></p>
13+
</pre><p><strong>Example 2:</strong></p>
1414
<pre><strong>Input:</strong> temperatures = [30,40,50,60]
1515
<strong>Output:</strong> [1,1,1,0]
16-
</pre><p><strong class="example">Example 3:</strong></p>
16+
</pre><p><strong>Example 3:</strong></p>
1717
<pre><strong>Input:</strong> temperatures = [30,60,90]
1818
<strong>Output:</strong> [1,1,0]
1919
</pre>

solution/0700-0799/0753.Cracking the Safe/README_EN.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
<li>After typing <code>5</code>, the most recent <code>3</code> digits is <code>&quot;345&quot;</code>, which is correct and the safe unlocks.</li>
2020
</ul>
2121
</li>
22-
2322
</ul>
2423

2524
<p>Return <em>any string of <strong>minimum length</strong> that will unlock the safe <strong>at some point</strong> of entering it</em>.</p>
2625

2726
<p>&nbsp;</p>
28-
<p><strong>Example 1:</strong></p>
27+
<p><strong class="example">Example 1:</strong></p>
2928

3029
<pre>
3130
<strong>Input:</strong> n = 1, k = 2
3231
<strong>Output:</strong> &quot;10&quot;
3332
<strong>Explanation:</strong> The password is a single digit, so enter each digit. &quot;01&quot; would also unlock the safe.
3433
</pre>
3534

36-
<p><strong>Example 2:</strong></p>
35+
<p><strong class="example">Example 2:</strong></p>
3736

3837
<pre>
3938
<strong>Input:</strong> n = 2, k = 2

solution/0900-0999/0940.Distinct Subsequences II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ int distinctSubseqII(char * s){
247247
for (int i = 0 ; i < n; i++) {
248248
int sum = 0;
249249
for (int j = 0; j < 26; j++) {
250-
sum = (sum + dp[j]) % mod;
250+
sum = (sum + dp[j]) % mod;
251251
}
252252
dp[s[i] - 'a'] = sum + 1;
253253
}

solution/0900-0999/0940.Distinct Subsequences II/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
<p>Given a string s, return <em>the number of <strong>distinct non-empty subsequences</strong> of</em> <code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
88
A <strong>subsequence</strong> of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;<u>a</u>b<u>c</u>d<u>e</u>&quot;</code> while <code>&quot;aec&quot;</code> is not.
99
<p>&nbsp;</p>
10-
<p><strong class="example">Example 1:</strong></p>
10+
<p><strong>Example 1:</strong></p>
1111

1212
<pre>
1313
<strong>Input:</strong> s = &quot;abc&quot;
1414
<strong>Output:</strong> 7
1515
<strong>Explanation:</strong> The 7 distinct subsequences are &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;ab&quot;, &quot;ac&quot;, &quot;bc&quot;, and &quot;abc&quot;.
1616
</pre>
1717

18-
<p><strong class="example">Example 2:</strong></p>
18+
<p><strong>Example 2:</strong></p>
1919

2020
<pre>
2121
<strong>Input:</strong> s = &quot;aba&quot;
2222
<strong>Output:</strong> 6
2323
<strong>Explanation:</strong> The 6 distinct subsequences are &quot;a&quot;, &quot;b&quot;, &quot;ab&quot;, &quot;aa&quot;, &quot;ba&quot;, and &quot;aba&quot;.
2424
</pre>
2525

26-
<p><strong class="example">Example 3:</strong></p>
26+
<p><strong>Example 3:</strong></p>
2727

2828
<pre>
2929
<strong>Input:</strong> s = &quot;aaa&quot;
@@ -213,7 +213,7 @@ int distinctSubseqII(char * s){
213213
for (int i = 0 ; i < n; i++) {
214214
int sum = 0;
215215
for (int j = 0; j < 26; j++) {
216-
sum = (sum + dp[j]) % mod;
216+
sum = (sum + dp[j]) % mod;
217217
}
218218
dp[s[i] - 'a'] = sum + 1;
219219
}

solution/1000-1099/1085.Sum of Digits in the Minimum Number/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
<p>Given an integer array <code>nums</code>, return <code>0</code><em> if the sum of the digits of the minimum integer in </em><code>nums</code><em> is odd, or </em><code>1</code><em> otherwise</em>.</p>
88

99
<p>&nbsp;</p>
10-
<p><strong class="example">Example 1:</strong></p>
10+
<p><strong>Example 1:</strong></p>
1111

1212
<pre>
1313
<strong>Input:</strong> nums = [34,23,1,24,75,33,54,8]
1414
<strong>Output:</strong> 0
1515
<strong>Explanation:</strong> The minimal element is 1, and the sum of those digits is 1 which is odd, so the answer is 0.
1616
</pre>
1717

18-
<p><strong class="example">Example 2:</strong></p>
18+
<p><strong>Example 2:</strong></p>
1919

2020
<pre>
2121
<strong>Input:</strong> nums = [99,77,33,66,55]

solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
<p>Return an <code>answer</code> array (of length <code>seq.length</code>) that encodes such a&nbsp;choice of <code>A</code> and <code>B</code>:&nbsp; <code>answer[i] = 0</code> if <code>seq[i]</code> is part of <code>A</code>, else <code>answer[i] = 1</code>.&nbsp; Note that even though multiple answers may exist, you may return any of them.</p>
3232

3333
<p>&nbsp;</p>
34-
<p><strong>Example 1:</strong></p>
34+
<p><strong class="example">Example 1:</strong></p>
3535

3636
<pre>
3737
<strong>Input:</strong> seq = &quot;(()())&quot;
3838
<strong>Output:</strong> [0,1,1,1,1,0]
3939
</pre>
4040

41-
<p><strong>Example 2:</strong></p>
41+
<p><strong class="example">Example 2:</strong></p>
4242

4343
<pre>
4444
<strong>Input:</strong> seq = &quot;()(())()&quot;

0 commit comments

Comments
 (0)