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,31 @@
|
||||
<p>You are given two integers, <code>n</code> and <code>k</code>.</p>
|
||||
|
||||
<p>An array of <strong>distinct</strong> positive integers is called a <b>k-avoiding</b> array if there does not exist any pair of distinct elements that sum to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible sum of a k-avoiding array of length </em><code>n</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, k = 4
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18.
|
||||
It can be proven that there is no k-avoiding array with a sum less than 18.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, k = 6
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can construct the array [1,2], which has a sum of 3.
|
||||
It can be proven that there is no k-avoiding array with a sum less than 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, k <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given an array of strings <code>words</code> and a string <code>s</code>, determine if <code>s</code> is an <strong>acronym</strong> of words.</p>
|
||||
|
||||
<p>The string <code>s</code> is considered an acronym of <code>words</code> if it can be formed by concatenating the <strong>first</strong> character of each string in <code>words</code> <strong>in order</strong>. For example, <code>"ab"</code> can be formed from <code>["apple", "banana"]</code>, but it can't be formed from <code>["bear", "aardvark"]</code>.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if </em><code>s</code><em> is an acronym of </em><code>words</code><em>, and </em><code>false</code><em> otherwise. </em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["alice","bob","charlie"], s = "abc"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["an","apple"], s = "a"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The first character in the words "an" and "apple" are 'a' and 'a', respectively.
|
||||
The acronym formed by concatenating these characters is "aa".
|
||||
Hence, s = "a" is not the acronym.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["never","gonna","give","up","on","you"], s = "ngguoy"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>By concatenating the first character of the words in the array, we get the string "ngguoy".
|
||||
Hence, s = "ngguoy" is the acronym.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>words[i]</code> and <code>s</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.<br />
|
||||
<br />
|
||||
The numbers from <code>0</code> to <code>n - 1</code> are divided into three groups numbered from <code>1</code> to <code>3</code>, where number <code>i</code> belongs to group <code>nums[i]</code>. Notice that some groups may be <strong>empty</strong>.<br />
|
||||
<br />
|
||||
You are allowed to perform this operation any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick number <code>x</code> and change its group. More formally, change <code>nums[x]</code> to any number from <code>1</code> to <code>3</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A new array <code>res</code> is constructed using the following procedure:</p>
|
||||
|
||||
<ol>
|
||||
<li>Sort the numbers in each group independently.</li>
|
||||
<li>Append the elements of groups <code>1</code>, <code>2</code>, and <code>3</code> to <code>res</code> <strong>in this order</strong>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Array <code>nums</code> is called a <strong>beautiful array</strong> if the constructed array <code>res</code> is sorted in <strong>non-decreasing</strong> order.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> a <strong>beautiful array</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,2,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> It's optimal to perform three operations:
|
||||
1. change nums[0] to 1.
|
||||
2. change nums[2] to 1.
|
||||
3. change nums[3] to 1.
|
||||
After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order.
|
||||
It can be proven that there is no valid sequence of less than three operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,1,3,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> It's optimal to perform two operations:
|
||||
1. change nums[1] to 1.
|
||||
2. change nums[2] to 1.
|
||||
After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.
|
||||
It can be proven that there is no valid sequence of less than two operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,2,2,3,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> It's optimal to not perform operations.
|
||||
After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 3</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given two <strong>0-indexed</strong> strings <code>str1</code> and <code>str2</code>.</p>
|
||||
|
||||
<p>In an operation, you select a <strong>set</strong> of indices in <code>str1</code>, and for each index <code>i</code> in the set, increment <code>str1[i]</code> to the next character <strong>cyclically</strong>. That is <code>'a'</code> becomes <code>'b'</code>, <code>'b'</code> becomes <code>'c'</code>, and so on, and <code>'z'</code> becomes <code>'a'</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to make </em><code>str2</code> <em>a subsequence of </em><code>str1</code> <em>by performing the operation <strong>at most once</strong></em>, <em>and</em> <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> str1 = "abc", str2 = "ad"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Select index 2 in str1.
|
||||
Increment str1[2] to become 'd'.
|
||||
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> str1 = "zc", str2 = "ad"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Select indices 0 and 1 in str1.
|
||||
Increment str1[0] to become 'a'.
|
||||
Increment str1[1] to become 'd'.
|
||||
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> str1 = "ab", str2 = "d"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once.
|
||||
Therefore, false is returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= str1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= str2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>str1</code> and <code>str2</code> consist of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A subarray is called <strong>equal</strong> if all of its elements are equal. Note that the empty subarray is an <strong>equal</strong> subarray.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> possible equal subarray after deleting <strong>at most</strong> </em><code>k</code><em> elements from </em><code>nums</code>.</p>
|
||||
|
||||
<p>A <b>subarray</b> is a contiguous, possibly empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,3,1,3], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> It's optimal to delete the elements at index 2 and index 4.
|
||||
After deleting them, nums becomes equal to [1, 3, 3, 3].
|
||||
The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
|
||||
It can be proven that no longer equal subarrays can be created.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,2,1,1], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> It's optimal to delete the elements at index 2 and index 3.
|
||||
After deleting them, nums becomes equal to [1, 1, 1, 1].
|
||||
The array itself is an equal subarray, so the answer is 4.
|
||||
It can be proven that no longer equal subarrays can be created.
|
||||
</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] <= nums.length</code></li>
|
||||
<li><code>0 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>You are given an array <code>nums</code> of <code>n</code> positive integers and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Initially, you start with a score of <code>1</code>. You have to maximize your score by applying the following operation at most <code>k</code> times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any <strong>non-empty</strong> subarray <code>nums[l, ..., r]</code> that you haven't chosen previously.</li>
|
||||
<li>Choose an element <code>x</code> of <code>nums[l, ..., r]</code> with the highest <strong>prime score</strong>. If multiple such elements exist, choose the one with the smallest index.</li>
|
||||
<li>Multiply your score by <code>x</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Here, <code>nums[l, ..., r]</code> denotes the subarray of <code>nums</code> starting at index <code>l</code> and ending at the index <code>r</code>, both ends being inclusive.</p>
|
||||
|
||||
<p>The <strong>prime score</strong> of an integer <code>x</code> is equal to the number of distinct prime factors of <code>x</code>. For example, the prime score of <code>300</code> is <code>3</code> since <code>300 = 2 * 2 * 3 * 5 * 5</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible score</strong> after applying at most </em><code>k</code><em> operations</em>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it modulo <code>10<sup>9 </sup>+ 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,3,9,3,8], k = 2
|
||||
<strong>Output:</strong> 81
|
||||
<strong>Explanation:</strong> To get a score of 81, we can apply the following operations:
|
||||
- Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9.
|
||||
- Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81.
|
||||
It can be proven that 81 is the highest score one can obtain.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [19,12,14,6,10,18], k = 3
|
||||
<strong>Output:</strong> 4788
|
||||
<strong>Explanation:</strong> To get a score of 4788, we can apply the following operations:
|
||||
- Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19.
|
||||
- Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342.
|
||||
- Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788.
|
||||
It can be proven that 4788 is the highest score one can obtain.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= min(n * (n + 1) / 2, 10<sup>9</sup>)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to find the <strong>maximum</strong> sum of a pair of numbers from <code>nums</code> such that the maximum <strong>digit </strong>in both numbers are equal.</p>
|
||||
|
||||
<p>Return <em>the maximum sum or</em> <code>-1</code><em> if no such pair exists</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [51,71,17,24,42]
|
||||
<strong>Output:</strong> 88
|
||||
<strong>Explanation:</strong>
|
||||
For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88.
|
||||
For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.
|
||||
It can be shown that there are no other pairs with equal maximum digits, so the answer is 88.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> No pair exists in nums with equal maximum digits.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, return <em>the number of pairs</em> <code>(i, j)</code> <em>where</em> <code>0 <= i < j < n</code> <em>and</em> <code>nums[i] + nums[j] < target</code>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,1,2,3,1], target = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 pairs of indices that satisfy the conditions in the statement:
|
||||
- (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target
|
||||
- (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target
|
||||
- (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target
|
||||
Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-6,2,5,-2,-7,-1,3], target = -2
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 pairs of indices that satisfy the conditions in the statement:
|
||||
- (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target
|
||||
- (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target
|
||||
- (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target
|
||||
- (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target
|
||||
- (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target
|
||||
- (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target
|
||||
- (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target
|
||||
- (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target
|
||||
- (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target
|
||||
- (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == n <= 50</code></li>
|
||||
<li><code>-50 <= nums[i], target <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p>
|
||||
|
||||
<p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,8,9]
|
||||
<strong>Output:</strong> [3,7,8]
|
||||
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [9,9,9]
|
||||
<strong>Output:</strong> [1,9,9,8]
|
||||
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
|
||||
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
|
||||
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given positive integers <code>low</code>, <code>high</code>, and <code>k</code>.</p>
|
||||
|
||||
<p>A number is <strong>beautiful</strong> if it meets both of the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The count of even digits in the number is equal to the count of odd digits.</li>
|
||||
<li>The number is divisible by <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of beautiful integers in the range</em> <code>[low, high]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 10, high = 20, k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful integers in the given range: [12,18].
|
||||
- 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
|
||||
- 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
|
||||
Additionally we can see that:
|
||||
- 16 is not beautiful because it is not divisible by k = 3.
|
||||
- 15 is not beautiful because it does not contain equal counts even and odd digits.
|
||||
It can be shown that there are only 2 beautiful integers in the given range.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 1, high = 10, k = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is 1 beautiful integer in the given range: [10].
|
||||
- 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1.
|
||||
It can be shown that there is only 1 beautiful integer in the given range.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 5, high = 5, k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 0 beautiful integers in the given range.
|
||||
- 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 < low <= high <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 < k <= 20</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given an integer <code>n</code> representing the number of houses on a number line, numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>Additionally, you are given a 2D integer array <code>offers</code> where <code>offers[i] = [start<sub>i</sub>, end<sub>i</sub>, gold<sub>i</sub>]</code>, indicating that <code>i<sup>th</sup></code> buyer wants to buy all the houses from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> for <code>gold<sub>i</sub></code> amount of gold.</p>
|
||||
|
||||
<p>As a salesman, your goal is to <strong>maximize</strong> your earnings by strategically selecting and selling houses to buyers.</p>
|
||||
|
||||
<p>Return <em>the maximum amount of gold you can earn</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that different buyers can't buy the same house, and some houses may remain unsold.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 5 houses numbered from 0 to 4 and there are 3 purchase offers.
|
||||
We sell houses in the range [0,0] to 1<sup>st</sup> buyer for 1 gold and houses in the range [1,3] to 3<sup>rd</sup> buyer for 2 golds.
|
||||
It can be proven that 3 is the maximum amount of gold we can achieve.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 5 houses numbered from 0 to 4 and there are 3 purchase offers.
|
||||
We sell houses in the range [0,2] to 2<sup>nd</sup> buyer for 10 golds.
|
||||
It can be proven that 10 is the maximum amount of gold we can achieve.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= offers.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>offers[i].length == 3</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>1 <= gold<sub>i</sub> <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>x</code>.</p>
|
||||
|
||||
<p>Find the <strong>minimum absolute difference</strong> between two elements in the array that are at least <code>x</code> indices apart.</p>
|
||||
|
||||
<p>In other words, find two indices <code>i</code> and <code>j</code> such that <code>abs(i - j) >= x</code> and <code>abs(nums[i] - nums[j])</code> is minimized.</p>
|
||||
|
||||
<p>Return<em> an integer denoting the <strong>minimum</strong> absolute difference between two elements that are at least</em> <code>x</code> <em>indices apart</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,2,4], x = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We can select nums[0] = 4 and nums[3] = 4.
|
||||
They are at least 2 indices apart, and their absolute difference is the minimum, 0.
|
||||
It can be shown that 0 is the optimal answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,3,2,10,15], x = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can select nums[1] = 3 and nums[2] = 2.
|
||||
They are at least 1 index apart, and their absolute difference is the minimum, 1.
|
||||
It can be shown that 1 is the optimal answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], x = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can select nums[0] = 1 and nums[3] = 4.
|
||||
They are at least 3 indices apart, and their absolute difference is the minimum, 3.
|
||||
It can be shown that 3 is the optimal answer.
|
||||
</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>0 <= x < nums.length</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user