Skip to content

Commit 1e27c89

Browse files
committed
Added problem description
1 parent fab5c6c commit 1e27c89

File tree

1,805 files changed

+71316
-0
lines changed

Some content is hidden

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

1,805 files changed

+71316
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<h3 align="left"> 542. 01 Matrix</h3>
2+
<div><p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p>
3+
4+
<p>The distance between two adjacent cells is <code>1</code>.</p>
5+
6+
<p>&nbsp;</p>
7+
<p><strong>Example 1:</strong></p>
8+
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;">
9+
<pre><strong>Input:</strong> mat = [[0,0,0],[0,1,0],[0,0,0]]
10+
<strong>Output:</strong> [[0,0,0],[0,1,0],[0,0,0]]
11+
</pre>
12+
13+
<p><strong>Example 2:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;">
15+
<pre><strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]]
16+
<strong>Output:</strong> [[0,0,0],[0,1,0],[1,2,1]]
17+
</pre>
18+
19+
<p>&nbsp;</p>
20+
<p><strong>Constraints:</strong></p>
21+
22+
<ul>
23+
<li><code>m == mat.length</code></li>
24+
<li><code>n == mat[i].length</code></li>
25+
<li><code>1 &lt;= m, n &lt;= 10<sup>4</sup></code></li>
26+
<li><code>1 &lt;= m * n &lt;= 10<sup>4</sup></code></li>
27+
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
28+
<li>There is at least one <code>0</code> in <code>mat</code>.</li>
29+
</ul>
30+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<h3 align="left"> 717. 1-bit and 2-bit Characters</h3>
2+
<div><p>We have two special characters:</p>
3+
4+
<ul>
5+
<li>The first character can be represented by one bit <code>0</code>.</li>
6+
<li>The second character can be represented by two bits (<code>10</code> or <code>11</code>).</li>
7+
</ul>
8+
9+
<p>Given a binary array <code>bits</code> that ends with <code>0</code>, return <code>true</code> if the last character must be a one-bit character.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre><strong>Input:</strong> bits = [1,0,0]
15+
<strong>Output:</strong> true
16+
<strong>Explanation:</strong> The only way to decode it is two-bit character and one-bit character.
17+
So the last character is one-bit character.
18+
</pre>
19+
20+
<p><strong>Example 2:</strong></p>
21+
22+
<pre><strong>Input:</strong> bits = [1,1,1,0]
23+
<strong>Output:</strong> false
24+
<strong>Explanation:</strong> The only way to decode it is two-bit character and two-bit character.
25+
So the last character is not one-bit character.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= bits.length &lt;= 1000</code></li>
33+
<li><code>bits[i]</code> is either <code>0</code> or <code>1</code>.</li>
34+
</ul>
35+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<h3 align="left"> 456. 132 Pattern</h3>
2+
<div><p>Given an array of <code>n</code> integers <code>nums</code>, a <strong>132 pattern</strong> is a subsequence of three integers <code>nums[i]</code>, <code>nums[j]</code> and <code>nums[k]</code> such that <code>i &lt; j &lt; k</code> and <code>nums[i] &lt; nums[k] &lt; nums[j]</code>.</p>
3+
4+
<p>Return <code>true</code><em> if there is a <strong>132 pattern</strong> in </em><code>nums</code><em>, otherwise, return </em><code>false</code><em>.</em></p>
5+
6+
<p>&nbsp;</p>
7+
<p><strong>Example 1:</strong></p>
8+
9+
<pre><strong>Input:</strong> nums = [1,2,3,4]
10+
<strong>Output:</strong> false
11+
<strong>Explanation:</strong> There is no 132 pattern in the sequence.
12+
</pre>
13+
14+
<p><strong>Example 2:</strong></p>
15+
16+
<pre><strong>Input:</strong> nums = [3,1,4,2]
17+
<strong>Output:</strong> true
18+
<strong>Explanation:</strong> There is a 132 pattern in the sequence: [1, 4, 2].
19+
</pre>
20+
21+
<p><strong>Example 3:</strong></p>
22+
23+
<pre><strong>Input:</strong> nums = [-1,3,2,0]
24+
<strong>Output:</strong> true
25+
<strong>Explanation:</strong> There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>n == nums.length</code></li>
33+
<li><code>1 &lt;= n &lt;= 2 * 10<sup>5</sup></code></li>
34+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
35+
</ul>
36+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<h3 align="left"> 650. 2 Keys Keyboard</h3>
2+
<div><p>There is only one character <code>'A'</code> on the screen of a notepad. You can perform one of two operations on this notepad for each step:</p>
3+
4+
<ul>
5+
<li>Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).</li>
6+
<li>Paste: You can paste the characters which are copied last time.</li>
7+
</ul>
8+
9+
<p>Given an integer <code>n</code>, return <em>the minimum number of operations to get the character</em> <code>'A'</code> <em>exactly</em> <code>n</code> <em>times on the screen</em>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre><strong>Input:</strong> n = 3
15+
<strong>Output:</strong> 3
16+
<strong>Explanation:</strong> Initially, we have one character 'A'.
17+
In step 1, we use Copy All operation.
18+
In step 2, we use Paste operation to get 'AA'.
19+
In step 3, we use Paste operation to get 'AAA'.
20+
</pre>
21+
22+
<p><strong>Example 2:</strong></p>
23+
24+
<pre><strong>Input:</strong> n = 1
25+
<strong>Output:</strong> 0
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= n &lt;= 1000</code></li>
33+
</ul>
34+
</div>
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<h3 align="left"> 679. 24 Game</h3>
2+
<div><p>You are given an integer array <code>cards</code> of length <code>4</code>. You have four cards, each containing a number in the range <code>[1, 9]</code>. You should arrange the numbers on these cards in a mathematical expression using the operators <code>['+', '-', '*', '/']</code> and the parentheses <code>'('</code> and <code>')'</code> to get the value 24.</p>
3+
4+
<p>You are restricted with the following rules:</p>
5+
6+
<ul>
7+
<li>The division operator <code>'/'</code> represents real division, not integer division.
8+
9+
<ul>
10+
<li>For example, <code>4 / (1 - 2 / 3) = 4 / (1 / 3) = 12</code>.</li>
11+
</ul>
12+
</li>
13+
<li>Every operation done is between two numbers. In particular, we cannot use <code>'-'</code> as a unary operator.
14+
<ul>
15+
<li>For example, if <code>cards = [1, 1, 1, 1]</code>, the expression <code>"-1 - 1 - 1 - 1"</code> is <strong>not allowed</strong>.</li>
16+
</ul>
17+
</li>
18+
<li>You cannot concatenate numbers together
19+
<ul>
20+
<li>For example, if <code>cards = [1, 2, 1, 2]</code>, the expression <code>"12 + 12"</code> is not valid.</li>
21+
</ul>
22+
</li>
23+
</ul>
24+
25+
<p>Return <code>true</code> if you can get such expression that evaluates to <code>24</code>, and <code>false</code> otherwise.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
30+
<pre><strong>Input:</strong> cards = [4,1,8,7]
31+
<strong>Output:</strong> true
32+
<strong>Explanation:</strong> (8-4) * (7-1) = 24
33+
</pre>
34+
35+
<p><strong>Example 2:</strong></p>
36+
37+
<pre><strong>Input:</strong> cards = [1,2,1,2]
38+
<strong>Output:</strong> false
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>cards.length == 4</code></li>
46+
<li><code>1 &lt;= cards[i] &lt;= 9</code></li>
47+
</ul>
48+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<h3 align="left"> 16. 3Sum Closest</h3>
2+
<div><p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, find three integers in <code>nums</code> such that the sum is closest to <code>target</code>.</p>
3+
4+
<p>Return <em>the sum of the three integers</em>.</p>
5+
6+
<p>You may assume that each input would have exactly one solution.</p>
7+
8+
<p>&nbsp;</p>
9+
<p><strong>Example 1:</strong></p>
10+
11+
<pre><strong>Input:</strong> nums = [-1,2,1,-4], target = 1
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
14+
</pre>
15+
16+
<p><strong>Example 2:</strong></p>
17+
18+
<pre><strong>Input:</strong> nums = [0,0,0], target = 1
19+
<strong>Output:</strong> 0
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>3 &lt;= nums.length &lt;= 1000</code></li>
27+
<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
28+
<li><code>-10<sup>4</sup> &lt;= target &lt;= 10<sup>4</sup></code></li>
29+
</ul>
30+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<h3 align="left"> 923. 3Sum With Multiplicity</h3>
2+
<div><p>Given an integer array <code>arr</code>, and an integer <code>target</code>, return the number of tuples <code>i, j, k</code> such that <code>i &lt; j &lt; k</code> and <code>arr[i] + arr[j] + arr[k] == target</code>.</p>
3+
4+
<p>As the answer can be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
5+
6+
<p>&nbsp;</p>
7+
<p><strong>Example 1:</strong></p>
8+
9+
<pre><strong>Input:</strong> arr = [1,1,2,2,3,3,4,4,5,5], target = 8
10+
<strong>Output:</strong> 20
11+
<strong>Explanation: </strong>
12+
Enumerating by the values (arr[i], arr[j], arr[k]):
13+
(1, 2, 5) occurs 8 times;
14+
(1, 3, 4) occurs 8 times;
15+
(2, 2, 4) occurs 2 times;
16+
(2, 3, 3) occurs 2 times.
17+
</pre>
18+
19+
<p><strong>Example 2:</strong></p>
20+
21+
<pre><strong>Input:</strong> arr = [1,1,2,2,2,2], target = 5
22+
<strong>Output:</strong> 12
23+
<strong>Explanation: </strong>
24+
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
25+
We choose one 1 from [1,1] in 2 ways,
26+
and two 2s from [2,2,2,2] in 6 ways.
27+
</pre>
28+
29+
<p><strong>Example 3:</strong></p>
30+
31+
<pre><strong>Input:</strong> arr = [2,1,3], target = 6
32+
<strong>Output:</strong> 1
33+
<strong>Explanation:</strong> (1, 2, 3) occured one time in the array so we return 1.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>3 &lt;= arr.length &lt;= 3000</code></li>
41+
<li><code>0 &lt;= arr[i] &lt;= 100</code></li>
42+
<li><code>0 &lt;= target &lt;= 300</code></li>
43+
</ul>
44+
</div>

scripts/algorithms/0-9/3Sum/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h3 align="left"> 15. 3Sum</h3>
2+
<div><p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
3+
4+
<p>Notice that the solution set must not contain duplicate triplets.</p>
5+
6+
<p>&nbsp;</p>
7+
<p><strong>Example 1:</strong></p>
8+
9+
<pre><strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
10+
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
11+
<strong>Explanation:</strong>
12+
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
13+
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
14+
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
15+
The distinct triplets are [-1,0,1] and [-1,-1,2].
16+
Notice that the order of the output and the order of the triplets does not matter.
17+
</pre>
18+
19+
<p><strong>Example 2:</strong></p>
20+
21+
<pre><strong>Input:</strong> nums = [0,1,1]
22+
<strong>Output:</strong> []
23+
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
24+
</pre>
25+
26+
<p><strong>Example 3:</strong></p>
27+
28+
<pre><strong>Input:</strong> nums = [0,0,0]
29+
<strong>Output:</strong> [[0,0,0]]
30+
<strong>Explanation:</strong> The only possible triplet sums up to 0.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>3 &lt;= nums.length &lt;= 3000</code></li>
38+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
39+
</ul>
40+
</div>
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<h3 align="left"> 454. 4Sum II</h3>
2+
<div><p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
3+
4+
<ul>
5+
<li><code>0 &lt;= i, j, k, l &lt; n</code></li>
6+
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
7+
</ul>
8+
9+
<p>&nbsp;</p>
10+
<p><strong>Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
13+
<strong>Output:</strong> 2
14+
<strong>Explanation:</strong>
15+
The two tuples are:
16+
1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
17+
2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
18+
</pre>
19+
20+
<p><strong>Example 2:</strong></p>
21+
22+
<pre><strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
23+
<strong>Output:</strong> 1
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>n == nums1.length</code></li>
31+
<li><code>n == nums2.length</code></li>
32+
<li><code>n == nums3.length</code></li>
33+
<li><code>n == nums4.length</code></li>
34+
<li><code>1 &lt;= n &lt;= 200</code></li>
35+
<li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li>
36+
</ul>
37+
</div>

scripts/algorithms/0-9/4Sum/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<h3 align="left"> 18. 4Sum</h3>
2+
<div><p>Given an array <code>nums</code> of <code>n</code> integers, return <em>an array of all the <strong>unique</strong> quadruplets</em> <code>[nums[a], nums[b], nums[c], nums[d]]</code> such that:</p>
3+
4+
<ul>
5+
<li><code>0 &lt;= a, b, c, d&nbsp;&lt; n</code></li>
6+
<li><code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code> are <strong>distinct</strong>.</li>
7+
<li><code>nums[a] + nums[b] + nums[c] + nums[d] == target</code></li>
8+
</ul>
9+
10+
<p>You may return the answer in <strong>any order</strong>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong>Example 1:</strong></p>
14+
15+
<pre><strong>Input:</strong> nums = [1,0,-1,0,-2,2], target = 0
16+
<strong>Output:</strong> [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
17+
</pre>
18+
19+
<p><strong>Example 2:</strong></p>
20+
21+
<pre><strong>Input:</strong> nums = [2,2,2,2,2], target = 8
22+
<strong>Output:</strong> [[2,2,2,2]]
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
30+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
31+
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
32+
</ul>
33+
</div>

0 commit comments

Comments
 (0)