mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
更新国外版新增题目
This commit is contained in:
parent
bc30d9a0a5
commit
7646741306
File diff suppressed because it is too large
Load Diff
186
leetcode/originData/count-lattice-points-inside-a-circle.json
Normal file
186
leetcode/originData/count-lattice-points-inside-a-circle.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
154
leetcode/originData/count-prefixes-of-a-given-string.json
Normal file
154
leetcode/originData/count-prefixes-of-a-given-string.json
Normal file
File diff suppressed because one or more lines are too long
194
leetcode/originData/number-of-flowers-in-full-bloom.json
Normal file
194
leetcode/originData/number-of-flowers-in-full-bloom.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
157
leetcode/originData/total-appeal-of-a-string.json
Normal file
157
leetcode/originData/total-appeal-of-a-string.json
Normal file
File diff suppressed because one or more lines are too long
41
leetcode/problem/count-lattice-points-inside-a-circle.html
Normal file
41
leetcode/problem/count-lattice-points-inside-a-circle.html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>Given a 2D integer array <code>circles</code> where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> represents the center <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code> of the <code>i<sup>th</sup></code> circle drawn on a grid, return <em>the <strong>number of lattice points</strong> </em><em>that are present inside <strong>at least one</strong> circle</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>lattice point</strong> is a point with integer coordinates.</li>
|
||||
<li>Points that lie <strong>on the circumference of a circle</strong> are also considered to be inside it.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/exa-11.png" style="width: 300px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> circles = [[2,2,1]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
The figure above shows the given circle.
|
||||
The lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green.
|
||||
Other points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle.
|
||||
Hence, the number of lattice points present inside at least one circle is 5.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/exa-22.png" style="width: 300px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> circles = [[2,2,2],[3,4,1]]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
The figure above shows the given circles.
|
||||
There are exactly 16 lattice points which are present inside at least one circle.
|
||||
Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= circles.length <= 200</code></li>
|
||||
<li><code>circles[i].length == 3</code></li>
|
||||
<li><code>1 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code></li>
|
||||
<li><code>1 <= r<sub>i</sub> <= min(x<sub>i</sub>, y<sub>i</sub>)</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a 2D integer array <code>rectangles</code> where <code>rectangles[i] = [l<sub>i</sub>, h<sub>i</sub>]</code> indicates that <code>i<sup>th</sup></code> rectangle has a length of <code>l<sub>i</sub></code> and a height of <code>h<sub>i</sub></code>. You are also given a 2D integer array <code>points</code> where <code>points[j] = [x<sub>j</sub>, y<sub>j</sub>]</code> is a point with coordinates <code>(x<sub>j</sub>, y<sub>j</sub>)</code>.</p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> rectangle has its <strong>bottom-left corner</strong> point at the coordinates <code>(0, 0)</code> and its <strong>top-right corner</strong> point at <code>(l<sub>i</sub>, h<sub>i</sub>)</code>.</p>
|
||||
|
||||
<p>Return<em> an integer array </em><code>count</code><em> of length </em><code>points.length</code><em> where </em><code>count[j]</code><em> is the number of rectangles that <strong>contain</strong> the </em><code>j<sup>th</sup></code><em> point.</em></p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> rectangle <strong>contains</strong> the <code>j<sup>th</sup></code> point if <code>0 <= x<sub>j</sub> <= l<sub>i</sub></code> and <code>0 <= y<sub>j</sub> <= h<sub>i</sub></code>. Note that points that lie on the <strong>edges</strong> of a rectangle are also considered to be contained by that rectangle.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/example1.png" style="width: 300px; height: 509px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> rectangles = [[1,2],[2,3],[2,5]], points = [[2,1],[1,4]]
|
||||
<strong>Output:</strong> [2,1]
|
||||
<strong>Explanation:</strong>
|
||||
The first rectangle contains no points.
|
||||
The second rectangle contains only the point (2, 1).
|
||||
The third rectangle contains the points (2, 1) and (1, 4).
|
||||
The number of rectangles that contain the point (2, 1) is 2.
|
||||
The number of rectangles that contain the point (1, 4) is 1.
|
||||
Therefore, we return [2, 1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/example2.png" style="width: 300px; height: 312px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> rectangles = [[1,1],[2,2],[3,3]], points = [[1,3],[1,1]]
|
||||
<strong>Output:</strong> [1,3]
|
||||
<strong>Explanation:
|
||||
</strong>The first rectangle contains only the point (1, 1).
|
||||
The second rectangle contains only the point (1, 1).
|
||||
The third rectangle contains the points (1, 3) and (1, 1).
|
||||
The number of rectangles that contain the point (1, 3) is 1.
|
||||
The number of rectangles that contain the point (1, 1) is 3.
|
||||
Therefore, we return [1, 3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= rectangles.length, points.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>rectangles[i].length == points[j].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub>, x<sub>j</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= h<sub>i</sub>, y<sub>j</sub> <= 100</code></li>
|
||||
<li>All the <code>rectangles</code> are <strong>unique</strong>.</li>
|
||||
<li>All the <code>points</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
34
leetcode/problem/count-prefixes-of-a-given-string.html
Normal file
34
leetcode/problem/count-prefixes-of-a-given-string.html
Normal file
@ -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>
|
38
leetcode/problem/count-unguarded-cells-in-the-grid.html
Normal file
38
leetcode/problem/count-unguarded-cells-in-the-grid.html
Normal file
@ -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>
|
57
leetcode/problem/escape-the-spreading-fire.html
Normal file
57
leetcode/problem/escape-the-spreading-fire.html
Normal file
@ -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>
|
28
leetcode/problem/intersection-of-multiple-arrays.html
Normal file
28
leetcode/problem/intersection-of-multiple-arrays.html
Normal file
@ -0,0 +1,28 @@
|
||||
Given a 2D integer array <code>nums</code> where <code>nums[i]</code> is a non-empty array of <strong>distinct</strong> positive integers, return <em>the list of integers that are present in <strong>each array</strong> of</em> <code>nums</code><em> sorted in <strong>ascending order</strong></em>.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[<u><strong>3</strong></u>,1,2,<u><strong>4</strong></u>,5],[1,2,<u><strong>3</strong></u>,<u><strong>4</strong></u>],[<u><strong>3</strong></u>,<u><strong>4</strong></u>,5,6]]
|
||||
<strong>Output:</strong> [3,4]
|
||||
<strong>Explanation:</strong>
|
||||
The only integers present in each of nums[0] = [<u><strong>3</strong></u>,1,2,<u><strong>4</strong></u>,5], nums[1] = [1,2,<u><strong>3</strong></u>,<u><strong>4</strong></u>], and nums[2] = [<u><strong>3</strong></u>,<u><strong>4</strong></u>,5,6] are 3 and 4, so we return [3,4].</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[4,5,6]]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong>
|
||||
There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= sum(nums[i].length) <= 1000</code></li>
|
||||
<li><code>1 <= nums[i][j] <= 1000</code></li>
|
||||
<li>All the values of <code>nums[i]</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
44
leetcode/problem/k-divisible-elements-subarrays.html
Normal file
44
leetcode/problem/k-divisible-elements-subarrays.html
Normal file
@ -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>
|
47
leetcode/problem/minimum-average-difference.html
Normal file
47
leetcode/problem/minimum-average-difference.html
Normal file
@ -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>
|
28
leetcode/problem/minimum-consecutive-cards-to-pick-up.html
Normal file
28
leetcode/problem/minimum-consecutive-cards-to-pick-up.html
Normal file
@ -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>
|
33
leetcode/problem/number-of-flowers-in-full-bloom.html
Normal file
33
leetcode/problem/number-of-flowers-in-full-bloom.html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>flowers</code>, where <code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means the <code>i<sup>th</sup></code> flower will be in <strong>full bloom</strong> from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> (<strong>inclusive</strong>). You are also given a <strong>0-indexed</strong> integer array <code>persons</code> of size <code>n</code>, where <code>persons[i]</code> is the time that the <code>i<sup>th</sup></code> person will arrive to see the flowers.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>n</code><em>, where </em><code>answer[i]</code><em> is the <strong>number</strong> of flowers that are in full bloom when the </em><code>i<sup>th</sup></code><em> person arrives.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg" style="width: 550px; height: 216px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
|
||||
<strong>Output:</strong> [1,2,2,2]
|
||||
<strong>Explanation: </strong>The figure above shows the times when the flowers are in full bloom and when the people arrive.
|
||||
For each person, we return the number of flowers in full bloom during their arrival.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg" style="width: 450px; height: 195px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> flowers = [[1,10],[3,3]], persons = [3,3,2]
|
||||
<strong>Output:</strong> [2,2,1]
|
||||
<strong>Explanation:</strong> The figure above shows the times when the flowers are in full bloom and when the people arrive.
|
||||
For each person, we return the number of flowers in full bloom during their arrival.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= flowers.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>flowers[i].length == 2</code></li>
|
||||
<li><code>1 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= persons.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= persons[i] <= 10<sup>9</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>
|
45
leetcode/problem/total-appeal-of-a-string.html
Normal file
45
leetcode/problem/total-appeal-of-a-string.html
Normal file
@ -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>
|
Loading…
Reference in New Issue
Block a user