mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 16:01:41 +08:00
update
This commit is contained in:
30
leetcode/problem/array-partition.html
Normal file
30
leetcode/problem/array-partition.html
Normal file
@@ -0,0 +1,30 @@
|
||||
<p>Given an integer array <code>nums</code> of <code>2n</code> integers, group these integers into <code>n</code> pairs <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> such that the sum of <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> for all <code>i</code> is <strong>maximized</strong>. Return<em> the maximized sum</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,3,2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> All possible pairings (ignoring the ordering of elements) are:
|
||||
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
|
||||
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
|
||||
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
|
||||
So the maximum possible sum is 4.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,2,6,5,1,2]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>nums.length == 2 * n</code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
48
leetcode/problem/count-the-number-of-ideal-arrays.html
Normal file
48
leetcode/problem/count-the-number-of-ideal-arrays.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>You are given two integers <code>n</code> and <code>maxValue</code>, which are used to describe an <strong>ideal</strong> array.</p>
|
||||
|
||||
<p>A <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is considered <strong>ideal</strong> if the following conditions hold:</p>
|
||||
|
||||
<ul>
|
||||
<li>Every <code>arr[i]</code> is a value from <code>1</code> to <code>maxValue</code>, for <code>0 <= i < n</code>.</li>
|
||||
<li>Every <code>arr[i]</code> is divisible by <code>arr[i - 1]</code>, for <code>0 < i < n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> ideal arrays of length </em><code>n</code>. Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, maxValue = 5
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> The following are the possible ideal arrays:
|
||||
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
|
||||
- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
|
||||
- Arrays starting with the value 3 (1 array): [3,3]
|
||||
- Arrays starting with the value 4 (1 array): [4,4]
|
||||
- Arrays starting with the value 5 (1 array): [5,5]
|
||||
There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, maxValue = 3
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The following are the possible ideal arrays:
|
||||
- Arrays starting with the value 1 (9 arrays):
|
||||
- With no other distinct values (1 array): [1,1,1,1,1]
|
||||
- With 2<sup>nd</sup> distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
|
||||
- With 2<sup>nd</sup> distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
|
||||
- Arrays starting with the value 2 (1 array): [2,2,2,2,2]
|
||||
- Arrays starting with the value 3 (1 array): [3,3,3,3,3]
|
||||
There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= maxValue <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
44
leetcode/problem/decode-the-message.html
Normal file
44
leetcode/problem/decode-the-message.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>You are given the strings <code>key</code> and <code>message</code>, which represent a cipher key and a secret message, respectively. The steps to decode <code>message</code> are as follows:</p>
|
||||
|
||||
<ol>
|
||||
<li>Use the <strong>first</strong> appearance of all 26 lowercase English letters in <code>key</code> as the <strong>order</strong> of the substitution table.</li>
|
||||
<li>Align the substitution table with the regular English alphabet.</li>
|
||||
<li>Each letter in <code>message</code> is then <strong>substituted</strong> using the table.</li>
|
||||
<li>Spaces <code>' '</code> are transformed to themselves.</li>
|
||||
</ol>
|
||||
|
||||
<ul>
|
||||
<li>For example, given <code>key = "<u><strong>hap</strong></u>p<u><strong>y</strong></u> <u><strong>bo</strong></u>y"</code> (actual key would have <strong>at least one</strong> instance of each letter in the alphabet), we have the partial substitution table of (<code>'h' -> 'a'</code>, <code>'a' -> 'b'</code>, <code>'p' -> 'c'</code>, <code>'y' -> 'd'</code>, <code>'b' -> 'e'</code>, <code>'o' -> 'f'</code>).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the decoded message</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/08/ex1new4.jpg" style="width: 752px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
|
||||
<strong>Output:</strong> "this is a secret"
|
||||
<strong>Explanation:</strong> The diagram above shows the substitution table.
|
||||
It is obtained by taking the first appearance of each letter in "<u><strong>the</strong></u> <u><strong>quick</strong></u> <u><strong>brown</strong></u> <u><strong>f</strong></u>o<u><strong>x</strong></u> <u><strong>j</strong></u>u<u><strong>mps</strong></u> o<u><strong>v</strong></u>er the <u><strong>lazy</strong></u> <u><strong>d</strong></u>o<u><strong>g</strong></u>".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/08/ex2new.jpg" style="width: 754px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
|
||||
<strong>Output:</strong> "the five boxing wizards jump quickly"
|
||||
<strong>Explanation:</strong> The diagram above shows the substitution table.
|
||||
It is obtained by taking the first appearance of each letter in "<u><strong>eljuxhpwnyrdgtqkviszcfmabo</strong></u>".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>26 <= key.length <= 2000</code></li>
|
||||
<li><code>key</code> consists of lowercase English letters and <code>' '</code>.</li>
|
||||
<li><code>key</code> contains every letter in the English alphabet (<code>'a'</code> to <code>'z'</code>) <strong>at least once</strong>.</li>
|
||||
<li><code>1 <= message.length <= 2000</code></li>
|
||||
<li><code>message</code> consists of lowercase English letters and <code>' '</code>.</li>
|
||||
</ul>
|
49
leetcode/problem/evaluate-boolean-binary-tree.html
Normal file
49
leetcode/problem/evaluate-boolean-binary-tree.html
Normal file
@@ -0,0 +1,49 @@
|
||||
<p>You are given the <code>root</code> of a <strong>full binary tree</strong> with the following properties:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Leaf nodes</strong> have either the value <code>0</code> or <code>1</code>, where <code>0</code> represents <code>False</code> and <code>1</code> represents <code>True</code>.</li>
|
||||
<li><strong>Non-leaf nodes</strong> have either the value <code>2</code> or <code>3</code>, where <code>2</code> represents the boolean <code>OR</code> and <code>3</code> represents the boolean <code>AND</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>evaluation</strong> of a node is as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>If the node is a leaf node, the evaluation is the <strong>value</strong> of the node, i.e. <code>True</code> or <code>False</code>.</li>
|
||||
<li>Otherwise, <strong>evaluate</strong> the node's two children and <strong>apply</strong> the boolean operation of its value with the children's evaluations.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the boolean result of <strong>evaluating</strong> the </em><code>root</code><em> node.</em></p>
|
||||
|
||||
<p>A <strong>full binary tree</strong> is a binary tree where each node has either <code>0</code> or <code>2</code> children.</p>
|
||||
|
||||
<p>A <strong>leaf node</strong> is a node that has zero children.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/16/example1drawio1.png" style="width: 700px; height: 252px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [2,1,3,null,null,0,1]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram illustrates the evaluation process.
|
||||
The AND node evaluates to False AND True = False.
|
||||
The OR node evaluates to True OR False = True.
|
||||
The root node evaluates to True, so we return true.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [0]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The root node is a leaf node and it evaluates to false, so we return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 3</code></li>
|
||||
<li>Every node has either <code>0</code> or <code>2</code> children.</li>
|
||||
<li>Leaf nodes have a value of <code>0</code> or <code>1</code>.</li>
|
||||
<li>Non-leaf nodes have a value of <code>2</code> or <code>3</code>.</li>
|
||||
</ul>
|
48
leetcode/problem/minimum-amount-of-time-to-fill-cups.html
Normal file
48
leetcode/problem/minimum-amount-of-time-to-fill-cups.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up <code>2</code> cups with <strong>different</strong> types of water, or <code>1</code> cup of any type of water.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>amount</code> of length <code>3</code> where <code>amount[0]</code>, <code>amount[1]</code>, and <code>amount[2]</code> denote the number of cold, warm, and hot water cups you need to fill respectively. Return <em>the <strong>minimum</strong> number of seconds needed to fill up all the cups</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> amount = [1,4,2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One way to fill up the cups is:
|
||||
Second 1: Fill up a cold cup and a warm cup.
|
||||
Second 2: Fill up a warm cup and a hot cup.
|
||||
Second 3: Fill up a warm cup and a hot cup.
|
||||
Second 4: Fill up a warm cup.
|
||||
It can be proven that 4 is the minimum number of seconds needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> amount = [5,4,4]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> One way to fill up the cups is:
|
||||
Second 1: Fill up a cold cup, and a hot cup.
|
||||
Second 2: Fill up a cold cup, and a warm cup.
|
||||
Second 3: Fill up a cold cup, and a warm cup.
|
||||
Second 4: Fill up a warm cup, and a hot cup.
|
||||
Second 5: Fill up a cold cup, and a hot cup.
|
||||
Second 6: Fill up a cold cup, and a warm cup.
|
||||
Second 7: Fill up a hot cup.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> amount = [5,0,0]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> Every second, we fill up a cold cup.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>amount.length == 3</code></li>
|
||||
<li><code>0 <= amount[i] <= 100</code></li>
|
||||
</ul>
|
41
leetcode/problem/minimum-sum-of-squared-difference.html
Normal file
41
leetcode/problem/minimum-sum-of-squared-difference.html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>You are given two positive <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>
|
||||
|
||||
<p>The <strong>sum of squared difference</strong> of arrays <code>nums1</code> and <code>nums2</code> is defined as the <strong>sum</strong> of <code>(nums1[i] - nums2[i])<sup>2</sup></code> for each <code>0 <= i < n</code>.</p>
|
||||
|
||||
<p>You are also given two positive integers <code>k1</code> and <code>k2</code>. You can modify any of the elements of <code>nums1</code> by <code>+1</code> or <code>-1</code> at most <code>k1</code> times. Similarly, you can modify any of the elements of <code>nums2</code> by <code>+1</code> or <code>-1</code> at most <code>k2</code> times.</p>
|
||||
|
||||
<p>Return <em>the minimum <strong>sum of squared difference</strong> after modifying array </em><code>nums1</code><em> at most </em><code>k1</code><em> times and modifying array </em><code>nums2</code><em> at most </em><code>k2</code><em> times</em>.</p>
|
||||
|
||||
<p><strong>Note</strong>: You are allowed to modify the array elements to become <strong>negative</strong> integers.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0
|
||||
<strong>Output:</strong> 579
|
||||
<strong>Explanation:</strong> The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0.
|
||||
The sum of square difference will be: (1 - 2)<sup>2 </sup>+ (2 - 10)<sup>2 </sup>+ (3 - 20)<sup>2 </sup>+ (4 - 19)<sup>2</sup> = 579.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1
|
||||
<strong>Output:</strong> 43
|
||||
<strong>Explanation:</strong> One way to obtain the minimum sum of square difference is:
|
||||
- Increase nums1[0] once.
|
||||
- Increase nums2[2] once.
|
||||
The minimum of the sum of square difference will be:
|
||||
(2 - 5)<sup>2 </sup>+ (4 - 8)<sup>2 </sup>+ (10 - 7)<sup>2 </sup>+ (12 - 9)<sup>2</sup> = 43.
|
||||
Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k1, k2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
46
leetcode/problem/move-pieces-to-obtain-a-string.html
Normal file
46
leetcode/problem/move-pieces-to-obtain-a-string.html
Normal file
@@ -0,0 +1,46 @@
|
||||
<p>You are given two strings <code>start</code> and <code>target</code>, both of length <code>n</code>. Each string consists <strong>only</strong> of the characters <code>'L'</code>, <code>'R'</code>, and <code>'_'</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li>The characters <code>'L'</code> and <code>'R'</code> represent pieces, where a piece <code>'L'</code> can move to the <strong>left</strong> only if there is a <strong>blank</strong> space directly to its left, and a piece <code>'R'</code> can move to the <strong>right</strong> only if there is a <strong>blank</strong> space directly to its right.</li>
|
||||
<li>The character <code>'_'</code> represents a blank space that can be occupied by <strong>any</strong> of the <code>'L'</code> or <code>'R'</code> pieces.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to obtain the string</em> <code>target</code><em> by moving the pieces of the string </em><code>start</code><em> <strong>any</strong> number of times</em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = "_L__R__R_", target = "L______RR"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can obtain the string target from start by doing the following moves:
|
||||
- Move the first piece one step to the left, start becomes equal to "<strong>L</strong>___R__R_".
|
||||
- Move the last piece one step to the right, start becomes equal to "L___R___<strong>R</strong>".
|
||||
- Move the second piece three steps to the right, start becomes equal to "L______<strong>R</strong>R".
|
||||
Since it is possible to get the string target from start, we return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = "R_L_", target = "__LR"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The 'R' piece in the string start can move one step to the right to obtain "_<strong>R</strong>L_".
|
||||
After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = "_R", target = "R_"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == start.length == target.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>start</code> and <code>target</code> consist of the characters <code>'L'</code>, <code>'R'</code>, and <code>'_'</code>.</li>
|
||||
</ul>
|
40
leetcode/problem/number-of-increasing-paths-in-a-grid.html
Normal file
40
leetcode/problem/number-of-increasing-paths-in-a-grid.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, where you can move from a cell to any adjacent cell in all <code>4</code> directions.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>strictly</strong> <strong>increasing</strong> paths in the grid such that you can start from <strong>any</strong> cell and end at <strong>any</strong> cell. </em>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Two paths are considered different if they do not have exactly the same sequence of visited cells.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/10/griddrawio-4.png" style="width: 181px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1],[3,4]]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The strictly increasing paths are:
|
||||
- Paths with length 1: [1], [1], [3], [4].
|
||||
- Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
|
||||
- Paths with length 3: [1 -> 3 -> 4].
|
||||
The total number of paths is 4 + 3 + 1 = 8.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1],[2]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The strictly increasing paths are:
|
||||
- Paths with length 1: [1], [2].
|
||||
- Paths with length 2: [1 -> 2].
|
||||
The total number of paths is 2 + 1 = 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
40
leetcode/problem/number-of-people-aware-of-a-secret.html
Normal file
40
leetcode/problem/number-of-people-aware-of-a-secret.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>On day <code>1</code>, one person discovers a secret.</p>
|
||||
|
||||
<p>You are given an integer <code>delay</code>, which means that each person will <strong>share</strong> the secret with a new person <strong>every day</strong>, starting from <code>delay</code> days after discovering the secret. You are also given an integer <code>forget</code>, which means that each person will <strong>forget</strong> the secret <code>forget</code> days after discovering it. A person <strong>cannot</strong> share the secret on the same day they forgot it, or on any day afterwards.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return<em> the number of people who know the secret at the end of day </em><code>n</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, delay = 2, forget = 4
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Day 1: Suppose the first person is named A. (1 person)
|
||||
Day 2: A is the only person who knows the secret. (1 person)
|
||||
Day 3: A shares the secret with a new person, B. (2 people)
|
||||
Day 4: A shares the secret with a new person, C. (3 people)
|
||||
Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
|
||||
Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, delay = 1, forget = 3
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
Day 1: The first person is named A. (1 person)
|
||||
Day 2: A shares the secret with B. (2 people)
|
||||
Day 3: A and B share the secret with 2 new people, C and D. (4 people)
|
||||
Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>1 <= delay < forget <= n</code></li>
|
||||
</ul>
|
40
leetcode/problem/smallest-number-in-infinite-set.html
Normal file
40
leetcode/problem/smallest-number-in-infinite-set.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>You have a set which contains all positive integers <code>[1, 2, 3, 4, 5, ...]</code>.</p>
|
||||
|
||||
<p>Implement the <code>SmallestInfiniteSet</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>SmallestInfiniteSet()</code> Initializes the <strong>SmallestInfiniteSet</strong> object to contain <strong>all</strong> positive integers.</li>
|
||||
<li><code>int popSmallest()</code> <strong>Removes</strong> and returns the smallest integer contained in the infinite set.</li>
|
||||
<li><code>void addBack(int num)</code> <strong>Adds</strong> a positive integer <code>num</code> back into the infinite set, if it is <strong>not</strong> already in the infinite set.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
|
||||
[[], [2], [], [], [], [1], [], [], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, 1, 2, 3, null, 1, 4, 5]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
|
||||
smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made.
|
||||
smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
|
||||
smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
|
||||
smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
|
||||
smallestInfiniteSet.addBack(1); // 1 is added back to the set.
|
||||
smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
|
||||
// is the smallest number, and remove it from the set.
|
||||
smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
|
||||
smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 1000</code></li>
|
||||
<li>At most <code>1000</code> calls will be made <strong>in total</strong> to <code>popSmallest</code> and <code>addBack</code>.</li>
|
||||
</ul>
|
35
leetcode/problem/spiral-matrix-iv.html
Normal file
35
leetcode/problem/spiral-matrix-iv.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<p>You are given two integers <code>m</code> and <code>n</code>, which represent the dimensions of a matrix.</p>
|
||||
|
||||
<p>You are also given the <code>head</code> of a linked list of integers.</p>
|
||||
|
||||
<p>Generate an <code>m x n</code> matrix that contains the integers in the linked list presented in <strong>spiral</strong> order <strong>(clockwise)</strong>, starting from the <strong>top-left</strong> of the matrix. If there are remaining empty spaces, fill them with <code>-1</code>.</p>
|
||||
|
||||
<p>Return <em>the generated matrix</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg" style="width: 240px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
|
||||
<strong>Output:</strong> [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
|
||||
<strong>Explanation:</strong> The diagram above shows how the values are printed in the matrix.
|
||||
Note that the remaining spaces in the matrix are filled with -1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg" style="width: 221px; height: 60px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 1, n = 4, head = [0,1,2]
|
||||
<strong>Output:</strong> [[0,1,2,-1]]
|
||||
<strong>Explanation:</strong> The diagram above shows how the values are printed from left to right in the matrix.
|
||||
The last space in the matrix is set to -1.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li>The number of nodes in the list is in the range <code>[1, m * n]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>threshold</code>.</p>
|
||||
|
||||
<p>Find any subarray of <code>nums</code> of length <code>k</code> such that <strong>every</strong> element in the subarray is <strong>greater</strong> than <code>threshold / k</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>size</strong> of <strong>any</strong> such subarray</em>. If there is no such subarray, return <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,4,3,1], threshold = 6
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2.
|
||||
Note that this is the only valid subarray.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,5,6,5,8], threshold = 7
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned.
|
||||
Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5.
|
||||
Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions.
|
||||
Therefore, 2, 3, 4, or 5 may also be returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], threshold <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
43
leetcode/problem/the-latest-time-to-catch-a-bus.html
Normal file
43
leetcode/problem/the-latest-time-to-catch-a-bus.html
Normal file
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>buses</code> of length <code>n</code>, where <code>buses[i]</code> represents the departure time of the <code>i<sup>th</sup></code> bus. You are also given a <strong>0-indexed</strong> integer array <code>passengers</code> of length <code>m</code>, where <code>passengers[j]</code> represents the arrival time of the <code>j<sup>th</sup></code> passenger. All bus departure times are unique. All passenger arrival times are unique.</p>
|
||||
|
||||
<p>You are given an integer <code>capacity</code>, which represents the <strong>maximum</strong> number of passengers that can get on each bus.</p>
|
||||
|
||||
<p>The passengers will get on the next available bus. You can get on a bus that will depart at <code>x</code> minutes if you arrive at <code>y</code> minutes where <code>y <= x</code>, and the bus is not full. Passengers with the <strong>earliest</strong> arrival times get on the bus first.</p>
|
||||
|
||||
<p>Return <em>the latest time you may arrive at the bus station to catch a bus</em>. You <strong>cannot</strong> arrive at the same time as another passenger.</p>
|
||||
|
||||
<p><strong>Note: </strong>The arrays <code>buses</code> and <code>passengers</code> are not necessarily sorted.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> buses = [10,20], passengers = [2,17,18,19], capacity = 2
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
The 1<sup>st</sup> bus departs with the 1<sup>st</sup> passenger.
|
||||
The 2<sup>nd</sup> bus departs with you and the 2<sup>nd</sup> passenger.
|
||||
Note that you must not arrive at the same time as the passengers, which is why you must arrive before the 2<sup>nd</sup><sup> </sup>passenger to catch the bus.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong>
|
||||
The 1<sup>st</sup> bus departs with the 4<sup>th</sup> passenger.
|
||||
The 2<sup>nd</sup> bus departs with the 6<sup>th</sup> and 2<sup>nd</sup><sup> </sup>passengers.
|
||||
The 3<sup>rd</sup> bus departs with the 1<sup>s</sup><sup>t</sup> passenger and you.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == buses.length</code></li>
|
||||
<li><code>m == passengers.length</code></li>
|
||||
<li><code>1 <= n, m, capacity <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= buses[i], passengers[i] <= 10<sup>9</sup></code></li>
|
||||
<li>Each element in <code>buses</code> is <strong>unique</strong>.</li>
|
||||
<li>Each element in <code>passengers</code> is <strong>unique</strong>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user