mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
更新国内版新增题目
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<p>Given an integer array <code>nums</code> and two integers <code>k</code> and <code>p</code>, return <em>the number of <strong>distinct subarrays</strong> which have <strong>at most</strong></em> <code>k</code> <em>elements divisible by</em> <code>p</code>.</p>
|
||||
|
||||
<p>Two arrays <code>nums1</code> and <code>nums2</code> are said to be <strong>distinct</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>They are of <strong>different</strong> lengths, or</li>
|
||||
<li>There exists <strong>at least</strong> one index <code>i</code> where <code>nums1[i] != nums2[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>subarray</strong> is defined as a <strong>non-empty</strong> contiguous sequence of elements in an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [<u><strong>2</strong></u>,3,3,<u><strong>2</strong></u>,<u><strong>2</strong></u>], k = 2, p = 2
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong>
|
||||
The elements at indices 0, 3, and 4 are divisible by p = 2.
|
||||
The 11 distinct subarrays which have at most k = 2 elements divisible by 2 are:
|
||||
[2], [2,3], [2,3,3], [2,3,3,2], [3], [3,3], [3,3,2], [3,3,2,2], [3,2], [3,2,2], and [2,2].
|
||||
Note that the subarrays [2] and [3] occur more than once in nums, but they should each be counted only once.
|
||||
The subarray [2,3,3,2,2] should not be counted because it has 3 elements that are divisible by 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], k = 4, p = 1
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong>
|
||||
All element of nums are divisible by p = 1.
|
||||
Also, every subarray of nums will have at most 4 elements that are divisible by 1.
|
||||
Since all subarrays are distinct, the total number of subarrays satisfying all the constraints is 10.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 200</code></li>
|
||||
<li><code>1 <= nums[i], p <= 200</code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>The <b>appeal</b> of a string is the number of <strong>distinct</strong> characters found in the string.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the appeal of <code>"abbca"</code> is <code>3</code> because it has <code>3</code> distinct characters: <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>s</code>, return <em>the <strong>total appeal of all of its <strong>substrings</strong>.</strong></em></p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abbca"
|
||||
<strong>Output:</strong> 28
|
||||
<strong>Explanation:</strong> The following are the substrings of "abbca":
|
||||
- Substrings of length 1: "a", "b", "b", "c", "a" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.
|
||||
- Substrings of length 2: "ab", "bb", "bc", "ca" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.
|
||||
- Substrings of length 3: "abb", "bbc", "bca" have an appeal of 2, 2, and 3 respectively. The sum is 7.
|
||||
- Substrings of length 4: "abbc", "bbca" have an appeal of 3 and 3 respectively. The sum is 6.
|
||||
- Substrings of length 5: "abbca" has an appeal of 3. The sum is 3.
|
||||
The total sum is 5 + 7 + 7 + 6 + 3 = 28.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "code"
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> The following are the substrings of "code":
|
||||
- Substrings of length 1: "c", "o", "d", "e" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.
|
||||
- Substrings of length 2: "co", "od", "de" have an appeal of 2, 2, and 2 respectively. The sum is 6.
|
||||
- Substrings of length 3: "cod", "ode" have an appeal of 3 and 3 respectively. The sum is 6.
|
||||
- Substrings of length 4: "code" has an appeal of 4. The sum is 4.
|
||||
The total sum is 4 + 6 + 6 + 4 = 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>You are given an integer array <code>cards</code> where <code>cards[i]</code> represents the <strong>value</strong> of the <code>i<sup>th</sup></code> card. A pair of cards are <strong>matching</strong> if the cards have the <strong>same</strong> value.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of <strong>consecutive</strong> cards you have to pick up to have a pair of <strong>matching</strong> cards among the picked cards.</em> If it is impossible to have matching cards, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cards = [3,4,2,3,4,7]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cards = [1,0,5,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no way to pick up a set of consecutive cards that contain a pair of matching cards.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= cards.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= cards[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
|
||||
|
||||
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
|
||||
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
|
||||
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,5,3,9,5,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
|
||||
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
|
||||
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
|
||||
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
|
||||
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
|
||||
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
|
||||
The average difference of index 3 is the minimum average difference so return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
The only index is 0 so return 0.
|
||||
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a string <code>number</code> representing a <strong>positive integer</strong> and a character <code>digit</code>.</p>
|
||||
|
||||
<p>Return <em>the resulting string after removing <strong>exactly one occurrence</strong> of </em><code>digit</code><em> from </em><code>number</code><em> such that the value of the resulting string in <strong>decimal</strong> form is <strong>maximized</strong></em>. The test cases are generated such that <code>digit</code> occurs at least once in <code>number</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> number = "123", digit = "3"
|
||||
<strong>Output:</strong> "12"
|
||||
<strong>Explanation:</strong> There is only one '3' in "123". After removing '3', the result is "12".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> number = "1231", digit = "1"
|
||||
<strong>Output:</strong> "231"
|
||||
<strong>Explanation:</strong> We can remove the first '1' to get "231" or remove the second '1' to get "123".
|
||||
Since 231 > 123, we return "231".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> number = "551", digit = "5"
|
||||
<strong>Output:</strong> "51"
|
||||
<strong>Explanation:</strong> We can remove either the first or second '5' from "551".
|
||||
Both result in the string "51".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= number.length <= 100</code></li>
|
||||
<li><code>number</code> consists of digits from <code>'1'</code> to <code>'9'</code>.</li>
|
||||
<li><code>digit</code> is a digit from <code>'1'</code> to <code>'9'</code>.</li>
|
||||
<li><code>digit</code> occurs at least once in <code>number</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given an integer <code>n</code>, return the count of all numbers with unique digits, <code>x</code>, where <code>0 <= x < 10<sup>n</sup></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 91
|
||||
<strong>Explanation:</strong> The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 0
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 8</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given a string array <code>words</code> and a string <code>s</code>, where <code>words[i]</code> and <code>s</code> comprise only of <strong>lowercase English letters</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of strings</strong> in</em> <code>words</code> <em>that are a <strong>prefix</strong> of</em> <code>s</code>.</p>
|
||||
|
||||
<p>A <strong>prefix</strong> of a string is a substring that occurs at the beginning of the string. A <b>substring</b> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","b","c","ab","bc","abc"], s = "abc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The strings in words which are a prefix of s = "abc" are:
|
||||
"a", "ab", and "abc".
|
||||
Thus the number of strings in words which are a prefix of s is 3.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","a"], s = "aa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:
|
||||
</strong>Both of the strings are a prefix of s.
|
||||
Note that the same string can occur multiple times in words, and it should be counted each time.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length, s.length <= 10</code></li>
|
||||
<li><code>words[i]</code> and <code>s</code> consist of lowercase English letters <strong>only</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given two integers <code>m</code> and <code>n</code> representing a <strong>0-indexed</strong> <code>m x n</code> grid. You are also given two 2D integer arrays <code>guards</code> and <code>walls</code> where <code>guards[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> and <code>walls[j] = [row<sub>j</sub>, col<sub>j</sub>]</code> represent the positions of the <code>i<sup>th</sup></code> guard and <code>j<sup>th</sup></code> wall respectively.</p>
|
||||
|
||||
<p>A guard can see <b>every</b> cell in the four cardinal directions (north, east, south, or west) starting from their position unless <strong>obstructed</strong> by a wall or another guard. A cell is <strong>guarded</strong> if there is <strong>at least</strong> one guard that can see it.</p>
|
||||
|
||||
<p>Return<em> the number of unoccupied cells that are <strong>not</strong> <strong>guarded</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png" style="width: 300px; height: 204px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The guarded and unguarded cells are shown in red and green respectively in the above diagram.
|
||||
There are a total of 7 unguarded cells, so we return 7.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png" style="width: 200px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The unguarded cells are shown in green in the above diagram.
|
||||
There are a total of 4 unguarded cells, so we return 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= guards.length, walls.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= guards.length + walls.length <= m * n</code></li>
|
||||
<li><code>guards[i].length == walls[j].length == 2</code></li>
|
||||
<li><code>0 <= row<sub>i</sub>, row<sub>j</sub> < m</code></li>
|
||||
<li><code>0 <= col<sub>i</sub>, col<sub>j</sub> < n</code></li>
|
||||
<li>All the positions in <code>guards</code> and <code>walls</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code> which represents a field. Each cell has one of three values:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> represents grass,</li>
|
||||
<li><code>1</code> represents fire,</li>
|
||||
<li><code>2</code> represents a wall that you and fire cannot pass through.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are situated in the top-left cell, <code>(0, 0)</code>, and you want to travel to the safehouse at the bottom-right cell, <code>(m - 1, n - 1)</code>. Every minute, you may move to an <strong>adjacent</strong> grass cell. <strong>After</strong> your move, every fire cell will spread to all <strong>adjacent</strong> cells that are not walls.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse</em>. If this is impossible, return <code>-1</code>. If you can <strong>always</strong> reach the safehouse regardless of the minutes stayed, return <code>10<sup>9</sup></code>.</p>
|
||||
|
||||
<p>Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse.</p>
|
||||
|
||||
<p>A cell is <strong>adjacent</strong> to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex1new.jpg" style="width: 650px; height: 404px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The figure above shows the scenario where you stay in the initial position for 3 minutes.
|
||||
You will still be able to safely reach the safehouse.
|
||||
Staying for more than 3 minutes will not allow you to safely reach the safehouse.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex2new2.jpg" style="width: 515px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The figure above shows the scenario where you immediately move towards the safehouse.
|
||||
Fire will spread to any cell you move towards and it is impossible to safely reach the safehouse.
|
||||
Thus, -1 is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex3new.jpg" style="width: 174px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,0],[2,2,0],[1,2,0]]
|
||||
<strong>Output:</strong> 1000000000
|
||||
<strong>Explanation:</strong> The figure above shows the initial grid.
|
||||
Notice that the fire is contained by walls and you will always be able to safely reach the safehouse.
|
||||
Thus, 10<sup>9</sup> is returned.
|
||||
</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>2 <= m, n <= 300</code></li>
|
||||
<li><code>4 <= m * n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code>, <code>1</code>, or <code>2</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user