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 <code>nums</code> containing <code>n</code> integers.</p>
|
||||
|
||||
<p>At each second, you perform the following operation on the array:</p>
|
||||
|
||||
<ul>
|
||||
<li>For every index <code>i</code> in the range <code>[0, n - 1]</code>, replace <code>nums[i]</code> with either <code>nums[i]</code>, <code>nums[(i - 1 + n) % n]</code>, or <code>nums[(i + 1) % n]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that all the elements get replaced simultaneously.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of seconds needed to make all elements in the array</em> <code>nums</code> <em>equal</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can equalize the array in 1 second in the following way:
|
||||
- At 1<sup>st</sup> second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2].
|
||||
It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,3,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can equalize the array in 2 seconds in the following way:
|
||||
- At 1<sup>st</sup> second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3].
|
||||
- At 2<sup>nd</sup> second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3].
|
||||
It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,5,5]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We don't need to perform any operations as all elements in the initial array are the same.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length. Every second, for all indices <code>0 <= i < nums1.length</code>, value of <code>nums1[i]</code> is incremented by <code>nums2[i]</code>. <strong>After</strong> this is done, you can do the following operation:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>0 <= i < nums1.length</code> and make <code>nums1[i] = 0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time in which you can make the sum of all elements of </em><code>nums1</code><em> to be<strong> less than or equal</strong> to </em><code>x</code>, <em>or </em><code>-1</code><em> if this is not possible.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,3], nums2 = [1,2,3], x = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6].
|
||||
For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9].
|
||||
For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0].
|
||||
Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,3], nums2 = [3,3,3], x = 4
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code><font face="monospace">1 <= nums1.length <= 10<sup>3</sup></font></code></li>
|
||||
<li><code>1 <= nums1[i] <= 10<sup>3</sup></code></li>
|
||||
<li><code>0 <= nums2[i] <= 10<sup>3</sup></code></li>
|
||||
<li><code>nums1.length == nums2.length</code></li>
|
||||
<li><code>0 <= x <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given an array <code>nums</code> of length <code>n</code> and an integer <code>m</code>. You need to determine if it is possible to split the array into <code>n</code> <strong>non-empty</strong> arrays by performing a series of steps.</p>
|
||||
|
||||
<p>In each step, you can select an existing array (which may be the result of previous steps) with a length of <strong>at least two</strong> and split it into <strong>two </strong>subarrays, if, <strong>for each </strong>resulting subarray, <strong>at least</strong> one of the following holds:</p>
|
||||
|
||||
<ul>
|
||||
<li>The length of the subarray is one, or</li>
|
||||
<li>The sum of elements of the subarray is <strong>greater than or equal</strong> to <code>m</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if you can split the given array into </em><code>n</code><em> arrays, otherwise return</em> <code>false</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> A subarray is <em>a contiguous non-empty sequence of elements within an array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2, 2, 1], m = 4
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can split the array into [2, 2] and [1] in the first step. Then, in the second step, we can split [2, 2] into [2] and [2]. As a result, the answer is true.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2, 1, 3], m = 5
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation: </strong>We can try splitting the array in two different ways: the first way is to have [2, 1] and [3], and the second way is to have [2] and [1, 3]. However, both of these ways are not valid. So, the answer is false.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2, 3, 3, 2, 3], m = 6
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can split the array into [2, 3, 3, 2] and [3] in the first step. Then, in the second step, we can split [2, 3, 3, 2] into [2, 3, 3] and [2]. Then, in the third step, we can split [2, 3, 3] into [2] and [3, 3]. And in the last step we can split [3, 3] into [3] and [3]. As a result, the answer is true.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li><code>1 <= m <= 200</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
Given three strings <code>a</code>, <code>b</code>, and <code>c</code>, your task is to find a string that has the<strong> minimum</strong> length and contains all three strings as <strong>substrings</strong>.
|
||||
<p>If there are multiple such strings, return the<em> </em><strong>lexicographically<em> </em>smallest </strong>one.</p>
|
||||
|
||||
<p>Return <em>a string denoting the answer to the problem.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A string <code>a</code> is <strong>lexicographically smaller</strong> than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears <strong>earlier </strong>in the alphabet than the corresponding letter in <code>b</code>.</li>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "abc", b = "bca", c = "aaa"
|
||||
<strong>Output:</strong> "aaabca"
|
||||
<strong>Explanation:</strong> We show that "aaabca" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and "aaabca" is the lexicographically smallest one.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "ab", b = "ba", c = "aba"
|
||||
<strong>Output:</strong> "aba"
|
||||
<strong>Explanation: </strong>We show that the string "aba" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that "aba" is the lexicographically smallest one.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a.length, b.length, c.length <= 100</code></li>
|
||||
<li><code>a</code>, <code>b</code>, <code>c</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Initially, you have a bank account balance of <code>100</code> dollars.</p>
|
||||
|
||||
<p>You are given an integer <code>purchaseAmount</code> representing the amount you will spend on a purchase in dollars.</p>
|
||||
|
||||
<p>At the store where you will make the purchase, the purchase amount is rounded to the <strong>nearest multiple</strong> of <code>10</code>. In other words, you pay a <strong>non-negative</strong> amount, <code>roundedAmount</code>, such that <code>roundedAmount</code> is a multiple of <code>10</code> and <code>abs(roundedAmount - purchaseAmount)</code> is <strong>minimized</strong>.</p>
|
||||
|
||||
<p>If there is more than one nearest multiple of <code>10</code>, the <strong>largest multiple</strong> is chosen.</p>
|
||||
|
||||
<p>Return <em>an integer denoting your account balance after making a purchase worth </em><code>purchaseAmount</code><em> dollars from the store.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> <code>0</code> is considered to be a multiple of <code>10</code> in this problem.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> purchaseAmount = 9
|
||||
<strong>Output:</strong> 90
|
||||
<strong>Explanation:</strong> In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> purchaseAmount = 15
|
||||
<strong>Output:</strong> 80
|
||||
<strong>Explanation:</strong> In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen.
|
||||
Hence, your account balance becomes 100 - 20 = 80.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= purchaseAmount <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You can do the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an integer <code>i</code> such that <code>0 <= i < nums.length - 1</code> and <code>nums[i] <= nums[i + 1]</code>. Replace the element <code>nums[i + 1]</code> with <code>nums[i] + nums[i + 1]</code> and delete the element <code>nums[i]</code> from the array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the value of the <b>largest</b> element that you can possibly obtain in the final array.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,7,9,3]
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong> We can apply the following operations on the array:
|
||||
- Choose i = 0. The resulting array will be nums = [<u>5</u>,7,9,3].
|
||||
- Choose i = 1. The resulting array will be nums = [5,<u>16</u>,3].
|
||||
- Choose i = 0. The resulting array will be nums = [<u>21</u>,3].
|
||||
The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,3,3]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We can do the following operations on the array:
|
||||
- Choose i = 1. The resulting array will be nums = [5,<u>6</u>].
|
||||
- Choose i = 0. The resulting array will be nums = [<u>11</u>].
|
||||
There is only one element in the final array, which is 11.
|
||||
</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>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given the head of a linked list <code>head</code>, in which each node contains an integer value.</p>
|
||||
|
||||
<p>Between every pair of adjacent nodes, insert a new node with a value equal to the <strong>greatest common divisor</strong> of them.</p>
|
||||
|
||||
<p>Return <em>the linked list after insertion</em>.</p>
|
||||
|
||||
<p>The <strong>greatest common divisor</strong> of two numbers is the largest positive integer that evenly divides both numbers.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png" style="width: 641px; height: 181px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [18,6,10,3]
|
||||
<strong>Output:</strong> [18,6,6,2,10,1,3]
|
||||
<strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).
|
||||
- We insert the greatest common divisor of 18 and 6 = 6 between the 1<sup>st</sup> and the 2<sup>nd</sup> nodes.
|
||||
- We insert the greatest common divisor of 6 and 10 = 2 between the 2<sup>nd</sup> and the 3<sup>rd</sup> nodes.
|
||||
- We insert the greatest common divisor of 10 and 3 = 1 between the 3<sup>rd</sup> and the 4<sup>th</sup> nodes.
|
||||
There are no more adjacent nodes, so we return the linked list.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png" style="width: 51px; height: 191px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [7]
|
||||
<strong>Output:</strong> [7]
|
||||
<strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes.
|
||||
There are no pairs of adjacent nodes, so we return the initial linked list.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>items</code> of length <code>n</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p><code>items[i] = [profit<sub>i</sub>, category<sub>i</sub>]</code>, where <code>profit<sub>i</sub></code> and <code>category<sub>i</sub></code> denote the profit and category of the <code>i<sup>th</sup></code> item respectively.</p>
|
||||
|
||||
<p>Let's define the <strong>elegance</strong> of a <strong>subsequence</strong> of <code>items</code> as <code>total_profit + distinct_categories<sup>2</sup></code>, where <code>total_profit</code> is the sum of all profits in the subsequence, and <code>distinct_categories</code> is the number of <strong>distinct</strong> categories from all the categories in the selected subsequence.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum elegance</strong> from all subsequences of size <code>k</code> in <code>items</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the maximum elegance of a subsequence of </em><code>items</code><em> with size exactly </em><code>k</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> items = [[3,2],[5,1],[10,1]], k = 2
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation: </strong>In this example, we have to select a subsequence of size 2.
|
||||
We can select items[0] = [3,2] and items[2] = [10,1].
|
||||
The total profit in this subsequence is 3 + 10 = 13, and the subsequence contains 2 distinct categories [2,1].
|
||||
Hence, the elegance is 13 + 2<sup>2</sup> = 17, and we can show that it is the maximum achievable elegance.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> items = [[3,1],[3,1],[2,2],[5,3]], k = 3
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> In this example, we have to select a subsequence of size 3.
|
||||
We can select items[0] = [3,1], items[2] = [2,2], and items[3] = [5,3].
|
||||
The total profit in this subsequence is 3 + 2 + 5 = 10, and the subsequence contains 3 distinct categories [1,2,3].
|
||||
Hence, the elegance is 10 + 3<sup>2</sup> = 19, and we can show that it is the maximum achievable elegance.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> items = [[1,1],[2,1],[3,1]], k = 3
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> In this example, we have to select a subsequence of size 3.
|
||||
We should select all the items.
|
||||
The total profit will be 1 + 2 + 3 = 6, and the subsequence contains 1 distinct category [1].
|
||||
Hence, the maximum elegance is 6 + 1<sup>2</sup> = 7. </pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= items.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>items[i].length == 2</code></li>
|
||||
<li><code>items[i][0] == profit<sub>i</sub></code></li>
|
||||
<li><code>items[i][1] == category<sub>i</sub></code></li>
|
||||
<li><code>1 <= profit<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= category<sub>i</sub> <= n </code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>Given two <strong>positive</strong> integers <code>n</code> and <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the number of ways </em><code>n</code><em> can be expressed as the sum of the </em><code>x<sup>th</sup></code><em> power of <strong>unique</strong> positive integers, in other words, the number of sets of unique integers </em><code>[n<sub>1</sub>, n<sub>2</sub>, ..., n<sub>k</sub>]</code><em> where </em><code>n = n<sub>1</sub><sup>x</sup> + n<sub>2</sub><sup>x</sup> + ... + n<sub>k</sub><sup>x</sup></code><em>.</em></p>
|
||||
|
||||
<p>Since the result can be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>For example, if <code>n = 160</code> and <code>x = 3</code>, one way to express <code>n</code> is <code>n = 2<sup>3</sup> + 3<sup>3</sup> + 5<sup>3</sup></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10, x = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can express n as the following: n = 3<sup>2</sup> + 1<sup>2</sup> = 10.
|
||||
It can be shown that it is the only way to express 10 as the sum of the 2<sup>nd</sup> power of unique integers.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, x = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can express n in the following ways:
|
||||
- n = 4<sup>1</sup> = 4.
|
||||
- n = 3<sup>1</sup> + 1<sup>1</sup> = 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 300</code></li>
|
||||
<li><code>1 <= x <= 5</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
|
||||
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the resulting string</em>.</p>
|
||||
|
||||
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "lEetcOde"
|
||||
<strong>Output:</strong> "lEOtcede"
|
||||
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "lYmpH"
|
||||
<strong>Output:</strong> "lYmpH"
|
||||
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
|
||||
</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 only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p>
|
||||
|
||||
<ul>
|
||||
<li>A cell containing a thief if <code>grid[r][c] = 1</code></li>
|
||||
<li>An empty cell if <code>grid[r][c] = 0</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p>
|
||||
|
||||
<p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p>
|
||||
|
||||
<p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p>
|
||||
|
||||
<p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
|
||||
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
|
||||
It can be shown that there are no other paths with a higher safeness factor.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since:
|
||||
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
|
||||
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
|
||||
It can be shown that there are no other paths with a higher safeness factor.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grid.length == n <= 400</code></li>
|
||||
<li><code>grid[i].length == n</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li>There is at least one thief in the <code>grid</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>Given an array of strings <code>words</code> and a character <code>separator</code>, <strong>split</strong> each string in <code>words</code> by <code>separator</code>.</p>
|
||||
|
||||
<p>Return <em>an array of strings containing the new strings formed after the splits, <strong>excluding empty strings</strong>.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>separator</code> is used to determine where the split should occur, but it is not included as part of the resulting strings.</li>
|
||||
<li>A split may result in more than two strings.</li>
|
||||
<li>The resulting strings must maintain the same order as they were initially given.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["one.two.three","four.five","six"], separator = "."
|
||||
<strong>Output:</strong> ["one","two","three","four","five","six"]
|
||||
<strong>Explanation: </strong>In this example we split as follows:
|
||||
|
||||
"one.two.three" splits into "one", "two", "three"
|
||||
"four.five" splits into "four", "five"
|
||||
"six" splits into "six"
|
||||
|
||||
Hence, the resulting array is ["one","two","three","four","five","six"].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["$easy$","$problem$"], separator = "$"
|
||||
<strong>Output:</strong> ["easy","problem"]
|
||||
<strong>Explanation:</strong> In this example we split as follows:
|
||||
|
||||
"$easy$" splits into "easy" (excluding empty strings)
|
||||
"$problem$" splits into "problem" (excluding empty strings)
|
||||
|
||||
Hence, the resulting array is ["easy","problem"].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["|||"], separator = "|"
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. </pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 20</code></li>
|
||||
<li>characters in <code>words[i]</code> are either lowercase English letters or characters from the string <code>".,|$#@"</code> (excluding the quotes)</li>
|
||||
<li><code>separator</code> is a character from the string <code>".,|$#@"</code> (excluding the quotes)</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Your laptop keyboard is faulty, and whenever you type a character <code>'i'</code> on it, it reverses the string that you have written. Typing other characters works as expected.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code>, and you type each character of <code>s</code> using your faulty keyboard.</p>
|
||||
|
||||
<p>Return <em>the final string that will be present on your laptop screen.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "string"
|
||||
<strong>Output:</strong> "rtsng"
|
||||
<strong>Explanation:</strong>
|
||||
After typing first character, the text on the screen is "s".
|
||||
After the second character, the text is "st".
|
||||
After the third character, the text is "str".
|
||||
Since the fourth character is an 'i', the text gets reversed and becomes "rts".
|
||||
After the fifth character, the text is "rtsn".
|
||||
After the sixth character, the text is "rtsng".
|
||||
Therefore, we return "rtsng".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "poiinter"
|
||||
<strong>Output:</strong> "ponter"
|
||||
<strong>Explanation:</strong>
|
||||
After the first character, the text on the screen is "p".
|
||||
After the second character, the text is "po".
|
||||
Since the third character you type is an 'i', the text gets reversed and becomes "op".
|
||||
Since the fourth character you type is an 'i', the text gets reversed and becomes "po".
|
||||
After the fifth character, the text is "pon".
|
||||
After the sixth character, the text is "pont".
|
||||
After the seventh character, the text is "ponte".
|
||||
After the eighth character, the text is "ponter".
|
||||
Therefore, we return "ponter".</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
<li><code>s[0] != 'i'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size <code>n</code>, where <code>parent[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parent[0] == -1</code>.</p>
|
||||
|
||||
<p>You are also given a string <code>s</code> of length <code>n</code>, where <code>s[i]</code> is the character assigned to the edge between <code>i</code> and <code>parent[i]</code>. <code>s[0]</code> can be ignored.</p>
|
||||
|
||||
<p>Return <em>the number of pairs of nodes </em><code>(u, v)</code><em> such that </em><code>u < v</code><em> and the characters assigned to edges on the path from </em><code>u</code><em> to </em><code>v</code><em> can be <strong>rearranged</strong> to form a <strong>palindrome</strong></em>.</p>
|
||||
|
||||
<p>A string is a <strong>palindrome</strong> when it reads the same backwards as forwards.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/07/15/treedrawio-8drawio.png" style="width: 281px; height: 181px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> parent = [-1,0,0,1,1,2], s = "acaabc"
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The valid pairs are:
|
||||
- All the pairs (0,1), (0,2), (1,3), (1,4) and (2,5) result in one character which is always a palindrome.
|
||||
- The pair (2,3) result in the string "aca" which is a palindrome.
|
||||
- The pair (1,5) result in the string "cac" which is a palindrome.
|
||||
- The pair (3,5) result in the string "acac" which can be rearranged into the palindrome "acca".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> parent = [-1,0,0,0,0], s = "aaaaa"
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> Any pair of nodes (u,v) where u < v is valid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == parent.length == s.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= parent[i] <= n - 1</code> for all <code>i >= 1</code></li>
|
||||
<li><code>parent[0] == -1</code></li>
|
||||
<li><code>parent</code> represents a valid tree.</li>
|
||||
<li><code>s</code> consists of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>nums</code>. We consider an array <strong>good </strong>if it is a permutation of an array <code>base[n]</code>.</p>
|
||||
|
||||
<p><code>base[n] = [1, 2, ..., n - 1, n, n] </code>(in other words, it is an array of length <code>n + 1</code> which contains <code>1</code> to <code>n - 1 </code>exactly once, plus two occurrences of <code>n</code>). For example, <code>base[1] = [1, 1]</code> and<code> base[3] = [1, 2, 3, 3]</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if the given array is good, otherwise return</em><em> </em><code>false</code>.</p>
|
||||
|
||||
<p><strong>Note: </strong>A permutation of integers represents an arrangement of these numbers.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2, 1, 3]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1, 3, 3, 2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1, 1]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3, 4, 4, 1, 2, 1]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= num[i] <= 200</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>There are <code>n</code> employees in a company, numbered from <code>0</code> to <code>n - 1</code>. Each employee <code>i</code> has worked for <code>hours[i]</code> hours in the company.</p>
|
||||
|
||||
<p>The company requires each employee to work for <strong>at least</strong> <code>target</code> hours.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> array of non-negative integers <code>hours</code> of length <code>n</code> and a non-negative integer <code>target</code>.</p>
|
||||
|
||||
<p>Return <em>the integer denoting the number of employees who worked at least</em> <code>target</code> <em>hours</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> hours = [0,1,2,3,4], target = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The company wants each employee to work for at least 2 hours.
|
||||
- Employee 0 worked for 0 hours and didn't meet the target.
|
||||
- Employee 1 worked for 1 hours and didn't meet the target.
|
||||
- Employee 2 worked for 2 hours and met the target.
|
||||
- Employee 3 worked for 3 hours and met the target.
|
||||
- Employee 4 worked for 4 hours and met the target.
|
||||
There are 3 employees who met the target.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> hours = [5,1,4,2,2], target = 6
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The company wants each employee to work for at least 6 hours.
|
||||
There are 0 employees who met the target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == hours.length <= 50</code></li>
|
||||
<li><code>0 <= hours[i], target <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>We call a subarray of an array <strong>complete</strong> if the following condition is satisfied:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number of <strong>distinct</strong> elements in the subarray is equal to the number of distinct elements in the whole array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>complete</strong> subarrays</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty part of an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,2,2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,5,5]
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 2000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>Given two positive integers <code>low</code> and <code>high</code> represented as strings, find the count of <strong>stepping numbers</strong> in the inclusive range <code>[low, high]</code>.</p>
|
||||
|
||||
<p>A <strong>stepping number</strong> is an integer such that all of its adjacent digits have an absolute difference of <strong>exactly</strong> <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the count of stepping numbers in the inclusive range</em> <code>[low, high]</code><em>. </em></p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> A stepping number should not have a leading zero.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = "1", high = "11"
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation: </strong>The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = "90", high = "101"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2. </pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= int(low) <= int(high) < 10<sup>100</sup></code></li>
|
||||
<li><code>1 <= low.length, high.length <= 100</code></li>
|
||||
<li><code>low</code> and <code>high</code> consist of only digits.</li>
|
||||
<li><code>low</code> and <code>high</code> don't have any leading zeros.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>x</code>.</p>
|
||||
|
||||
<p>You are <strong>initially</strong> at position <code>0</code> in the array and you can visit other positions according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>If you are currently in position <code>i</code>, then you can move to <strong>any</strong> position <code>j</code> such that <code>i < j</code>.</li>
|
||||
<li>For each position <code>i</code> that you visit, you get a score of <code>nums[i]</code>.</li>
|
||||
<li>If you move from a position <code>i</code> to a position <code>j</code> and the <strong>parities</strong> of <code>nums[i]</code> and <code>nums[j]</code> differ, then you lose a score of <code>x</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> total score you can get</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that initially you have <code>nums[0]</code> points.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,6,1,9,2], x = 5
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> We can visit the following positions in the array: 0 -> 2 -> 3 -> 4.
|
||||
The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5.
|
||||
The total score will be: 2 + 6 + 1 + 9 - 5 = 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,6,8], x = 3
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> All the integers in the array have the same parities, so we can visit all of them without losing any score.
|
||||
The total score is: 2 + 4 + 6 + 8 = 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], x <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>usageLimits</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>Your task is to create <strong>groups</strong> using numbers from <code>0</code> to <code>n - 1</code>, ensuring that each number, <code>i</code>, is used no more than <code>usageLimits[i]</code> times in total <strong>across all groups</strong>. You must also satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each group must consist of <strong>distinct </strong>numbers, meaning that no duplicate numbers are allowed within a single group.</li>
|
||||
<li>Each group (except the first one) must have a length <strong>strictly greater</strong> than the previous group.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> number of groups you can create while satisfying these conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> <code>usageLimits</code> = [1,2,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times.
|
||||
One way of creating the maximum number of groups while satisfying the conditions is:
|
||||
Group 1 contains the number [2].
|
||||
Group 2 contains the numbers [1,2].
|
||||
Group 3 contains the numbers [0,1,2].
|
||||
It can be shown that the maximum number of groups is 3.
|
||||
So, the output is 3. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> <code>usageLimits</code> = [2,1,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice.
|
||||
One way of creating the maximum number of groups while satisfying the conditions is:
|
||||
Group 1 contains the number [0].
|
||||
Group 2 contains the numbers [1,2].
|
||||
It can be shown that the maximum number of groups is 2.
|
||||
So, the output is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> <code>usageLimits</code> = [1,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, we can use both 0 and 1 at most once.
|
||||
One way of creating the maximum number of groups while satisfying the conditions is:
|
||||
Group 1 contains the number [0].
|
||||
It can be shown that the maximum number of groups is 1.
|
||||
So, the output is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= usageLimits.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= usageLimits[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user