mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p>
|
||||
|
||||
<p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <strong>if</strong> <code>|nums[i] - nums[j]| <= limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>lexicographically smallest array</strong> that can be obtained by performing the operation any number of times</em>.</p>
|
||||
|
||||
<p>An array <code>a</code> is lexicographically smaller than an array <code>b</code> if in the first position where <code>a</code> and <code>b</code> differ, array <code>a</code> has an element that is less than the corresponding element in <code>b</code>. For example, the array <code>[2,10,3]</code> is lexicographically smaller than the array <code>[10,2,3]</code> because they differ at index <code>0</code> and <code>2 < 10</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,3,9,8], limit = 2
|
||||
<strong>Output:</strong> [1,3,5,8,9]
|
||||
<strong>Explanation:</strong> Apply the operation 2 times:
|
||||
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
|
||||
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
|
||||
We cannot obtain a lexicographically smaller array by applying any more operations.
|
||||
Note that it may be possible to get the same result by doing different operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,7,6,18,2,1], limit = 3
|
||||
<strong>Output:</strong> [1,6,7,18,1,2]
|
||||
<strong>Explanation:</strong> Apply the operation 3 times:
|
||||
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
|
||||
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
|
||||
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
|
||||
We cannot obtain a lexicographically smaller array by applying any more operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,7,28,19,10], limit = 3
|
||||
<strong>Output:</strong> [1,7,28,19,10]
|
||||
<strong>Explanation:</strong> [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.
|
||||
</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] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= limit <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given three strings <code>s1</code>, <code>s2</code>, and <code>s3</code>. You have to perform the following operation on these three strings <strong>as many times</strong> as you want<!-- notionvc: b5178de7-3318-4129-b7d9-726b47e90621 -->.</p>
|
||||
|
||||
<p>In one operation you can choose one of these three strings such that its length is at least <code>2</code><!-- notionvc: 3342ac46-33c8-4010-aacd-e58678ce31ef --> and delete the <strong>rightmost</strong> character of it.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return </em><code>-1</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "abc", s2 = "abb", s3 = "ab"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Performing operations on s1 and s2 once will lead to three equal strings.
|
||||
It can be shown that there is no way to make them equal with less than two operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "dac", s2 = "bac", s3 = "cac"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Because the leftmost letters<!-- notionvc: 47239f7c-eec1-49f8-af79-c206ec88cb07 --> of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length, s3.length <= 100</code></li>
|
||||
<li><font face="monospace"><code>s1</code>,</font> <code><font face="monospace">s2</font></code><font face="monospace"> and</font> <code><font face="monospace">s3</font></code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p>
|
||||
|
||||
<p>In each step, you can choose two adjacent balls and swap them.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of steps to group all the black balls to the right and all the white balls to the left</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "101"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can group all the black balls to the right in the following way:
|
||||
- Swap s[0] and s[1], s = "011".
|
||||
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "100"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can group all the black balls to the right in the following way:
|
||||
- Swap s[0] and s[1], s = "010".
|
||||
- Swap s[1] and s[2], s = "001".
|
||||
It can be proven that the minimum number of steps needed is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0111"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All the black balls are already grouped to the right.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. You have to cyclically <strong>right</strong> shift <strong>odd</strong> indexed rows <code>k</code> times and cyclically <strong>left</strong> shift <strong>even</strong> indexed rows <code>k</code> times.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if the initial and final matrix are exactly the same and </em><code>false</code> <em>otherwise.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/29/similarmatrix.png" style="width: 500px; height: 117px;" />
|
||||
|
||||
Initially, the matrix looks like the first figure.
|
||||
Second figure represents the state of the matrix after one right and left cyclic shifts to even and odd indexed rows.
|
||||
Third figure is the final state of the matrix after two cyclic shifts which is similar to the initial matrix.
|
||||
Therefore, return true.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[2,2],[2,2]], k = 3
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same. Therefeore, we return true.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,2]], k = 1
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> After one cyclic shift, mat = [[2,1]] which is not equal to the initial matrix. Therefore we return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= mat.length <= 25</code></li>
|
||||
<li><code>1 <= mat[i].length <= 25</code></li>
|
||||
<li><code>1 <= mat[i][j] <= 25</code></li>
|
||||
<li><code>1 <= k <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>mountain</code>. Your task is to find all the <strong>peaks</strong> in the <code>mountain</code> array.</p>
|
||||
|
||||
<p>Return <em>an array that consists of </em>indices<!-- notionvc: c9879de8-88bd-43b0-8224-40c4bee71cd6 --><em> of <strong>peaks</strong> in the given array in <strong>any order</strong>.</em></p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>peak</strong> is defined as an element that is <strong>strictly greater</strong> than its neighboring elements.</li>
|
||||
<li>The first and last elements of the array are <strong>not</strong> a peak.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mountain = [2,4,4]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
|
||||
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
|
||||
So the answer is [].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mountain = [1,4,3,8,5]
|
||||
<strong>Output:</strong> [1,3]
|
||||
<strong>Explanation:</strong> mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
|
||||
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
|
||||
But mountain [1] and mountain[3] are strictly greater than their neighboring elements.
|
||||
So the answer is [1,3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= mountain.length <= 100</code></li>
|
||||
<li><code>1 <= mountain[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p>
|
||||
|
||||
<p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i < j</code> and <code>heights[i] < heights[j]</code>.</p>
|
||||
|
||||
<p>You are also given another array <code>queries</code> where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. On the <code>i<sup>th</sup></code> query, Alice is in building <code>a<sub>i</sub></code> while Bob is in building <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>an array</em> <code>ans</code> <em>where</em> <code>ans[i]</code> <em>is <strong>the index of the leftmost building</strong> where Alice and Bob can meet on the</em> <code>i<sup>th</sup></code> <em>query</em>. <em>If Alice and Bob cannot move to a common building on query</em> <code>i</code>, <em>set</em> <code>ans[i]</code> <em>to</em> <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
|
||||
<strong>Output:</strong> [2,5,-1,5,2]
|
||||
<strong>Explanation:</strong> In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2].
|
||||
In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5].
|
||||
In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
|
||||
In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5].
|
||||
In the fifth query, Alice and Bob are already in the same building.
|
||||
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
|
||||
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
|
||||
<strong>Output:</strong> [7,6,-1,4,6]
|
||||
<strong>Explanation:</strong> In the first query, Alice can directly move to Bob's building since heights[0] < heights[7].
|
||||
In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6].
|
||||
In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
|
||||
In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4].
|
||||
In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6].
|
||||
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
|
||||
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= heights.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= heights[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= heights.length - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
|
||||
|
||||
<p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,5,6]</code> and you select subarray <code>[3,5]</code> the array will convert to <code>[1,8,6]</code>.</p>
|
||||
|
||||
<p>Return <em>the </em><strong><em>maximum</em></strong><em> length of a </em><strong><em>non-decreasing</em></strong><em> array that can be made after applying operations.</em></p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,2,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> This array with length 3 is not non-decreasing.
|
||||
We have two ways to make the array length two.
|
||||
First, choosing subarray [2,2] converts the array to [5,4].
|
||||
Second, choosing subarray [5,2] converts the array to [7,2].
|
||||
In these two ways the array is not non-decreasing.
|
||||
And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing.
|
||||
So the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The array is non-decreasing. So the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,2,6]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.
|
||||
Because the given array is not non-decreasing, the maximum<!-- notionvc: 3447a505-d1ee-4411-8cae-e52162f53a55 --> possible answer is 3.</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] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,86 @@
|
||||
<p>There is a grid with <code>n + 2</code> <strong>horizontal</strong> bars and <code>m + 2</code> <strong>vertical</strong> bars, and initially containing <code>1 x 1</code> unit cells.</p>
|
||||
|
||||
<p>The bars are <strong>1-indexed</strong>.</p>
|
||||
|
||||
<p>You are given the two integers, <code>n</code> and <code>m</code>.</p>
|
||||
|
||||
<p>You are also given two integer arrays: <code>hBars</code> and <code>vBars</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>hBars</code> contains <strong>distinct</strong> horizontal bars in the range <code>[2, n + 1]</code>.</li>
|
||||
<li><code>vBars</code> contains <strong>distinct</strong> vertical bars in the range <code>[2, m + 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are allowed to <strong>remove</strong> bars that satisfy any of the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>If it is a horizontal bar, it must correspond to a value in <code>hBars</code>.</li>
|
||||
<li>If it is a vertical bar, it must correspond to a value in <code>vBars</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> area of a <strong>square-shaped</strong> hole in the grid after removing some bars (<strong>possibly none</strong>).</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-40-25.png" style="width: 411px; height: 220px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, m = 1, hBars = [2,3], vBars = [2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The left image shows the initial grid formed by the bars.
|
||||
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,3].
|
||||
It is allowed to remove horizontal bars [2,3] and the vertical bar [2].
|
||||
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
|
||||
The resulting grid is shown on the right.
|
||||
The hole has an area of 4.
|
||||
It can be shown that it is not possible to get a square hole with an area more than 4.
|
||||
Hence, the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/04/screenshot-from-2023-11-04-17-01-02.png" style="width: 368px; height: 145px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, m = 1, hBars = [2], vBars = [2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The left image shows the initial grid formed by the bars.
|
||||
The horizontal bars are in the range [1,3], and the vertical bars are in the range [1,3].
|
||||
It is allowed to remove the horizontal bar [2] and the vertical bar [2].
|
||||
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
|
||||
The resulting grid is shown on the right.
|
||||
The hole has an area of 4.
|
||||
Hence, the answer is 4, and it is the maximum possible.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-33-35.png" style="width: 648px; height: 218px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The left image shows the initial grid formed by the bars.
|
||||
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,5].
|
||||
It is allowed to remove horizontal bars [2,3] and vertical bars [2,3,4].
|
||||
One way to get the maximum square-shaped hole is by removing horizontal bars 2 and 3, and vertical bars 3 and 4.
|
||||
The resulting grid is shown on the right.
|
||||
The hole has an area of 9.
|
||||
It can be shown that it is not possible to get a square hole with an area more than 9.
|
||||
Hence, the answer is 9.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= hBars.length <= 100</code></li>
|
||||
<li><code>2 <= hBars[i] <= n + 1</code></li>
|
||||
<li><code>1 <= vBars.length <= 100</code></li>
|
||||
<li><code>2 <= vBars[i] <= m + 1</code></li>
|
||||
<li>All values in <code>hBars</code> are distinct.</li>
|
||||
<li>All values in <code>vBars</code> are distinct.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 <= x < 2<sup>n</sup></code>.</p>
|
||||
|
||||
<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 12, b = 5, n = 4
|
||||
<strong>Output:</strong> 98
|
||||
<strong>Explanation:</strong> For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) * (b XOR x) = 98.
|
||||
It can be shown that 98 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2<sup>n</sup><span style="font-size: 10.8333px;">.</span>
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 6, b = 7 , n = 5
|
||||
<strong>Output:</strong> 930
|
||||
<strong>Explanation:</strong> For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) * (b XOR x) = 930.
|
||||
It can be shown that 930 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2<sup>n</sup>.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 1, b = 6, n = 3
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) * (b XOR x) = 12.
|
||||
It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2<sup>n</sup>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= a, b < 2<sup>50</sup></code></li>
|
||||
<li><code>0 <= n <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a character <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>an <strong>array of indices</strong> representing the words that contain the character </em><code>x</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the returned array may be in <strong>any</strong> order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["leet","code"], x = "e"
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> "e" occurs in both words: "l<strong><u>ee</u></strong>t", and "cod<u><strong>e</strong></u>". Hence, we return indices 0 and 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","bcd","aaaa","cbc"], x = "a"
|
||||
<strong>Output:</strong> [0,2]
|
||||
<strong>Explanation:</strong> "a" occurs in "<strong><u>a</u></strong>bc", and "<u><strong>aaaa</strong></u>". Hence, we return indices 0 and 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","bcd","aaaa","cbc"], x = "z"
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> "z" does not occur in any of the words. Hence, we return an empty array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 50</code></li>
|
||||
<li><code>x</code> is a lowercase English letter.</li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a string <code>word</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A substring <code>s</code> of <code>word</code> is <strong>complete</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each character in <code>s</code> occurs <strong>exactly</strong> <code>k</code> times.</li>
|
||||
<li>The difference between two adjacent characters is <strong>at most</strong> <code>2</code>. That is, for any two adjacent characters <code>c1</code> and <code>c2</code> in <code>s</code>, the absolute difference in their positions in the alphabet is <strong>at most</strong> <code>2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>complete </strong>substrings of</em> <code>word</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a <strong>non-empty</strong> contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "igigee", k = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: <u><strong>igig</strong></u>ee, igig<u><strong>ee</strong></u>, <u><strong>igigee</strong></u>.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aaabbbccc", k = 3
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: <strong><u>aaa</u></strong>bbbccc, aaa<u><strong>bbb</strong></u>ccc, aaabbb<u><strong>ccc</strong></u>, <strong><u>aaabbb</u></strong>ccc, aaa<u><strong>bbbccc</strong></u>, <u><strong>aaabbbccc</strong></u>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given an integer <code>n</code> and a <strong>0-indexed</strong><strong> </strong>integer array <code>sick</code> which is <strong>sorted</strong> in <strong>increasing</strong> order.</p>
|
||||
|
||||
<p>There are <code>n</code> children standing in a queue with positions <code>0</code> to <code>n - 1</code> assigned to them. The array <code>sick</code> contains the positions of the children who are infected with an infectious disease. An infected child at position <code>i</code> can spread the disease to either of its immediate neighboring children at positions <code>i - 1</code> and <code>i + 1</code> <strong>if</strong> they exist and are currently not infected. <strong>At most one</strong> child who was previously not infected can get infected with the disease in one second.</p>
|
||||
|
||||
<p>It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An <strong>infection sequence</strong> is the sequential order of positions in which <strong>all</strong> of the non-infected children get infected with the disease. Return <em>the total number of possible infection sequences</em>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that an infection sequence <strong>does not</strong> contain positions of children who were already infected with the disease in the beginning.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, sick = [0,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences:
|
||||
- The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
|
||||
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected.
|
||||
Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3].
|
||||
- The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
|
||||
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected.
|
||||
Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2].
|
||||
- The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [<u>0</u>,1,2,3,<u>4</u>] => [<u>0</u>,1,2,<u>3</u>,<u>4</u>] => [<u>0</u>,<u>1</u>,2,<u>3</u>,<u>4</u>] => [<u>0</u>,<u>1</u>,<u>2</u>,<u>3</u>,<u>4</u>].
|
||||
- The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [<u>0</u>,1,2,3,<u>4</u>] => [<u>0</u>,1,2,<u>3</u>,<u>4</u>] => [<u>0</u>,1,<u>2</u>,<u>3</u>,<u>4</u>] => [<u>0</u>,<u>1</u>,<u>2</u>,<u>3</u>,<u>4</u>].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, sick = [1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences:
|
||||
- The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,<u>1</u>,2,3] => [<u>0</u>,<u>1</u>,2,3] => [<u>0</u>,<u>1</u>,<u>2</u>,3] => [<u>0</u>,<u>1</u>,<u>2</u>,<u>3</u>].
|
||||
- The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,<u>1</u>,2,3] => [0,<u>1</u>,<u>2</u>,3] => [<u>0</u>,<u>1</u>,<u>2</u>,3] => [<u>0</u>,<u>1</u>,<u>2</u>,<u>3</u>].
|
||||
- The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,<u>1</u>,2,3] => [0,<u>1</u>,<u>2</u>,3] => [0,<u>1</u>,<u>2</u>,<u>3</u>] => [<u>0</u>,<u>1</u>,<u>2</u>,<u>3</u>].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= sick.length <= n - 1</code></li>
|
||||
<li><code>0 <= sick[i] <= n - 1</code></li>
|
||||
<li><code>sick</code> is sorted in increasing order.</li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p>
|
||||
|
||||
<p>A string is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>vowels == consonants</code>.</li>
|
||||
<li><code>(vowels * consonants) % k == 0</code>, in other terms the multiplication of <code>vowels</code> and <code>consonants</code> is divisible by <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>non-empty beautiful substrings</strong> in the given string</em> <code>s</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p><strong>Vowel letters</strong> in English are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p><strong>Consonant letters</strong> in English are every letter except vowels.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "baeyh", k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful substrings in the given string.
|
||||
- Substring "b<u>aeyh</u>", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
|
||||
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
|
||||
- Substring "<u>baey</u>h", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
|
||||
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
|
||||
It can be shown that there are only 2 beautiful substrings in the given string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abba", k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 beautiful substrings in the given string.
|
||||
- Substring "<u>ab</u>ba", vowels = 1 (["a"]), consonants = 1 (["b"]).
|
||||
- Substring "ab<u>ba</u>", vowels = 1 (["a"]), consonants = 1 (["b"]).
|
||||
- Substring "<u>abba</u>", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
|
||||
It can be shown that there are only 3 beautiful substrings in the given string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bcdf", k = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no beautiful substrings in the given string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>1 <= k <= 1000</code></li>
|
||||
<li><code>s</code> consists of only English lowercase letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p>
|
||||
|
||||
<p>A string is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>vowels == consonants</code>.</li>
|
||||
<li><code>(vowels * consonants) % k == 0</code>, in other terms the multiplication of <code>vowels</code> and <code>consonants</code> is divisible by <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>non-empty beautiful substrings</strong> in the given string</em> <code>s</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p><strong>Vowel letters</strong> in English are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p><strong>Consonant letters</strong> in English are every letter except vowels.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "baeyh", k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful substrings in the given string.
|
||||
- Substring "b<u>aeyh</u>", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
|
||||
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
|
||||
- Substring "<u>baey</u>h", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
|
||||
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
|
||||
It can be shown that there are only 2 beautiful substrings in the given string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abba", k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 beautiful substrings in the given string.
|
||||
- Substring "<u>ab</u>ba", vowels = 1 (["a"]), consonants = 1 (["b"]).
|
||||
- Substring "ab<u>ba</u>", vowels = 1 (["a"]), consonants = 1 (["b"]).
|
||||
- Substring "<u>abba</u>", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
|
||||
It can be shown that there are only 3 beautiful substrings in the given string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bcdf", k = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no beautiful substrings in the given string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= k <= 1000</code></li>
|
||||
<li><code>s</code> consists of only English lowercase letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are at a fruit market with different types of exotic fruits on display.</p>
|
||||
|
||||
<p>You are given a <strong>1-indexed</strong> array <code>prices</code>, where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p>
|
||||
|
||||
<p>The fruit market has the following offer:</p>
|
||||
|
||||
<ul>
|
||||
<li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[i]</code> coins, you can get the next <code>i</code> fruits for free.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that even if you <strong>can</strong> take fruit <code>j</code> for free, you can still purchase it for <code>prices[j]</code> coins to receive a new offer.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of coins needed to acquire all the fruits</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [3,1,2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> You can acquire the fruits as follows:
|
||||
- Purchase the 1<sup>st</sup> fruit with 3 coins, you are allowed to take the 2<sup>nd</sup> fruit for free.
|
||||
- Purchase the 2<sup>nd</sup> fruit with 1 coin, you are allowed to take the 3<sup>rd</sup> fruit for free.
|
||||
- Take the 3<sup>rd</sup> fruit for free.
|
||||
Note that even though you were allowed to take the 2<sup>nd</sup> fruit for free, you purchased it because it is more optimal.
|
||||
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [1,10,1,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can acquire the fruits as follows:
|
||||
- Purchase the 1<sup>st</sup> fruit with 1 coin, you are allowed to take the 2<sup>nd</sup> fruit for free.
|
||||
- Take the 2<sup>nd</sup> fruit for free.
|
||||
- Purchase the 3<sup>rd</sup> fruit for 1 coin, you are allowed to take the 4<sup>th</sup> fruit for free.
|
||||
- Take the 4<sup>t</sup><sup>h</sup> fruit for free.
|
||||
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prices.length <= 1000</code></li>
|
||||
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>coins</code>, representing the values of the coins available, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is <strong>obtainable</strong> if there exists a subsequence of <code>coins</code> that sums to <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the<strong> minimum</strong> number of coins <strong>of any value</strong> that need to be added to the array so that every integer in the range</em> <code>[1, target]</code><em> is <strong>obtainable</strong></em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new <strong>non-empty</strong> array that is formed from the original array by deleting some (<strong>possibly none</strong>) of the elements without disturbing the relative positions of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,4,10], target = 19
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
|
||||
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,4,10,5,7,19], target = 19
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
|
||||
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,1,1], target = 20
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
|
||||
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= coins.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= coins[i] <= target</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user