mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-13 17:35:14 +08:00
update
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<p>You are given two integers <code>w</code> and <code>m</code>, and an integer array <code>arrivals</code>, where <code>arrivals[i]</code> is the type of item arriving on day <code>i</code> (days are <strong>1-indexed</strong>).</p>
|
||||
|
||||
<p>Items are managed according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each arrival may be <strong>kept</strong> or <strong>discarded</strong>; an item may only be discarded on its arrival day.</li>
|
||||
<li>For each day <code>i</code>, consider the window of days <code>[max(1, i - w + 1), i]</code> (the <code>w</code> most recent days up to day <code>i</code>):
|
||||
<ul>
|
||||
<li>For <strong>any</strong> such window, each item type may appear <strong>at most</strong> <code>m</code> times among kept arrivals whose arrival day lies in that window.</li>
|
||||
<li>If keeping the arrival on day <code>i</code> would cause its type to appear <strong>more than</strong> <code>m</code> times in the window, that arrival <strong>must</strong> be discarded.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of arrivals to be discarded so that every <code>w</code>-day window contains at most <code>m</code> occurrences of each type.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">arrivals = [1,2,1,3,1], w = 4, m = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>On day 1, Item 1 arrives; the window contains no more than <code>m</code> occurrences of this type, so we keep it.</li>
|
||||
<li>On day 2, Item 2 arrives; the window of days 1 - 2 is fine.</li>
|
||||
<li>On day 3, Item 1 arrives, window <code>[1, 2, 1]</code> has item 1 twice, within limit.</li>
|
||||
<li>On day 4, Item 3 arrives, window <code>[1, 2, 1, 3]</code> has item 1 twice, allowed.</li>
|
||||
<li>On day 5, Item 1 arrives, window <code>[2, 1, 3, 1]</code> has item 1 twice, still valid.</li>
|
||||
</ul>
|
||||
|
||||
<p>There are no discarded items, so return 0.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">arrivals = [1,2,3,3,3,4], w = 3, m = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>On day 1, Item 1 arrives. We keep it.</li>
|
||||
<li>On day 2, Item 2 arrives, window <code>[1, 2]</code> is fine.</li>
|
||||
<li>On day 3, Item 3 arrives, window <code>[1, 2, 3]</code> has item 3 once.</li>
|
||||
<li>On day 4, Item 3 arrives, window <code>[2, 3, 3]</code> has item 3 twice, allowed.</li>
|
||||
<li>On day 5, Item 3 arrives, window <code>[3, 3, 3]</code> has item 3 three times, exceeds limit, so the arrival must be discarded.</li>
|
||||
<li>On day 6, Item 4 arrives, window <code>[3, 4]</code> is fine.</li>
|
||||
</ul>
|
||||
|
||||
<p>Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arrivals.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arrivals[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= w <= arrivals.length</code></li>
|
||||
<li><code>1 <= m <= w</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>Return the bitwise <strong>OR</strong> of all <strong>even</strong> numbers in the array.</p>
|
||||
|
||||
<p>If there are no even numbers in <code>nums</code>, return 0.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,6]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The even numbers are 2, 4, and 6. Their bitwise OR equals 6.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [7,9,11]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are no even numbers, so the result is 0.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,8,16]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">24</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The even numbers are 8 and 16. Their bitwise OR equals 24.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p data-end="320" data-start="259">You are given an integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p data-end="294" data-start="163">An array <strong>capped</strong> by value <code>x</code> is obtained by replacing every element <code>nums[i]</code> with <code>min(nums[i], x)</code>.</p>
|
||||
|
||||
<p data-end="511" data-start="296">For each integer <code data-end="316" data-start="313">x</code> from 1 to <code data-end="332" data-start="329">n</code>, determine whether it is possible to choose a <strong><span data-keyword="subsequence-array-nonempty">subsequence</span></strong> from the array capped by <code>x</code> such that the sum of the chosen elements is <strong>exactly</strong> <code data-end="510" data-start="507">k</code>.</p>
|
||||
|
||||
<p data-end="788" data-start="649">Return a <strong>0-indexed</strong> boolean array <code data-end="680" data-start="672">answer</code> of size <code data-end="694" data-start="691">n</code>, where <code data-end="713" data-start="702">answer[i]</code> is <code data-end="723" data-start="717">true</code> if it is possible when using <code data-end="764" data-start="753">x = i + 1</code>, and <code data-end="777" data-start="770">false</code> otherwise.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,3,2,4], k = 5</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[false,false,true,true]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>x = 1</code>, the capped array is <code>[1, 1, 1, 1]</code>. Possible sums are <code>1, 2, 3, 4</code>, so it is impossible to form a sum of <code>5</code>.</li>
|
||||
<li>For <code>x = 2</code>, the capped array is <code>[2, 2, 2, 2]</code>. Possible sums are <code>2, 4, 6, 8</code>, so it is impossible to form a sum of <code>5</code>.</li>
|
||||
<li>For <code>x = 3</code>, the capped array is <code>[3, 3, 2, 3]</code>. A subsequence <code>[2, 3]</code> sums to <code>5</code>, so it is possible.</li>
|
||||
<li>For <code>x = 4</code>, the capped array is <code>[4, 3, 2, 4]</code>. A subsequence <code>[3, 2]</code> sums to <code>5</code>, so it is possible.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[true,true,true,true,true]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>For every value of <code>x</code>, it is always possible to select a subsequence from the capped array that sums exactly to <code>3</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 4000</code></li>
|
||||
<li><code>1 <= nums[i] <= n</code></li>
|
||||
<li><code>1 <= k <= 4000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>Return the <strong>smallest absent positive</strong> integer in <code>nums</code> such that it is <strong>strictly greater</strong> than the <strong>average</strong> of all elements in <code>nums</code>.</p>
|
||||
The <strong>average</strong> of an array is defined as the sum of all its elements divided by the number of elements.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The average of <code>nums</code> is <code>(3 + 5) / 2 = 8 / 2 = 4</code>.</li>
|
||||
<li>The smallest absent positive integer greater than 4 is 6.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [-1,1,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The average of <code>nums</code> is <code>(-1 + 1 + 2) / 3 = 2 / 3 = 0.667</code>.</li>
|
||||
<li>The smallest absent positive integer greater than 0.667 is 3.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,-1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The average of <code>nums</code> is <code>(4 + (-1)) / 2 = 3 / 2 = 1.50</code>.</li>
|
||||
<li>The smallest absent positive integer greater than 1.50 is 2.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>-100 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> where each element is a non-negative integer.</p>
|
||||
|
||||
<p>Select <strong>two</strong> <span data-keyword="subsequence-array">subsequences</span> of <code>nums</code> (they may be empty and are <strong>allowed</strong> to <strong>overlap</strong>), each preserving the original order of elements, and let:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>X</code> be the bitwise XOR of all elements in the first subsequence.</li>
|
||||
<li><code>Y</code> be the bitwise XOR of all elements in the second subsequence.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>maximum</strong> possible value of <code>X XOR Y</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> The XOR of an <strong>empty</strong> subsequence is 0.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Choose subsequences:</p>
|
||||
|
||||
<ul>
|
||||
<li>First subsequence <code>[2]</code>, whose XOR is 2.</li>
|
||||
<li>Second subsequence <code>[2,3]</code>, whose XOR is 1.</li>
|
||||
</ul>
|
||||
|
||||
<p>Then, XOR of both subsequences = <code>2 XOR 1 = 3</code>.</p>
|
||||
|
||||
<p>This is the maximum XOR value achievable from any two subsequences.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">7</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Choose subsequences:</p>
|
||||
|
||||
<ul>
|
||||
<li>First subsequence <code>[5]</code>, whose XOR is 5.</li>
|
||||
<li>Second subsequence <code>[2]</code>, whose XOR is 2.</li>
|
||||
</ul>
|
||||
|
||||
<p>Then, XOR of both subsequences = <code>5 XOR 2 = 7</code>.</p>
|
||||
|
||||
<p>This is the maximum XOR value achievable from any two subsequences.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a 2D integer array <code>tasks</code> where <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>Each <code>[s<sub>i</sub>, t<sub>i</sub>]</code> in <code>tasks</code> represents a task with start time <code>s<sub>i</sub></code> that takes <code>t<sub>i</sub></code> units of time to finish.</p>
|
||||
|
||||
<p>Return the earliest time at which at least one task is finished.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">tasks = [[1,6],[2,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The first task starts at time <code>t = 1</code> and finishes at time <code>1 + 6 = 7</code>. The second task finishes at time <code>2 + 3 = 5</code>. You can finish one task at time 5.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">tasks = [[100,100],[100,100],[100,100]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">200</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>All three tasks finish at time <code>100 + 100 = 200</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 100</code></li>
|
||||
<li><code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code></li>
|
||||
<li><code>1 <= s<sub>i</sub>, t<sub>i</sub> <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, each of length <code>n</code>. You may perform the following <strong>split-and-merge operation</strong> on <code>nums1</code> any number of times:</p>
|
||||
|
||||
<ol>
|
||||
<li>Choose a subarray <code>nums1[L..R]</code>.</li>
|
||||
<li>Remove that subarray, leaving the prefix <code>nums1[0..L-1]</code> (empty if <code>L = 0</code>) and the suffix <code>nums1[R+1..n-1]</code> (empty if <code>R = n - 1</code>).</li>
|
||||
<li>Re-insert the removed subarray (in its original order) at <strong>any</strong> position in the remaining array (i.e., between any two elements, at the very start, or at the very end).</li>
|
||||
</ol>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of <strong>split-and-merge operations</strong> needed to transform <code>nums1</code> into <code>nums2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums1 = [3,1,2], nums2 = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Split out the subarray <code>[3]</code> (<code>L = 0</code>, <code>R = 0</code>); the remaining array is <code>[1,2]</code>.</li>
|
||||
<li>Insert <code>[3]</code> at the end; the array becomes <code>[1,2,3]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums1 = </span>[1,1,2,3,4,5]<span class="example-io">, nums2 = </span>[5,4,3,2,1,1]</p>
|
||||
|
||||
<p><strong>Output: </strong>3</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Remove <code>[1,1,2]</code> at indices <code>0 - 2</code>; remaining is <code>[3,4,5]</code>; insert <code>[1,1,2]</code> at position <code>2</code>, resulting in <code>[3,4,1,1,2,5]</code>.</li>
|
||||
<li>Remove <code>[4,1,1]</code> at indices <code>1 - 3</code>; remaining is <code>[3,2,5]</code>; insert <code>[4,1,1]</code> at position <code>3</code>, resulting in <code>[3,2,5,4,1,1]</code>.</li>
|
||||
<li>Remove <code>[3,2]</code> at indices <code>0 - 1</code>; remaining is <code>[5,4,1,1]</code>; insert <code>[3,2]</code> at position <code>2</code>, resulting in <code>[5,4,3,2,1,1]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums1.length == nums2.length <= 6</code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums1[i], nums2[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums2</code> is a <strong>permutation</strong> of <code>nums1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>In one operation, choose any subarray <code>nums[l...r]</code> (<code>0 <= l <= r < n</code>) and <strong>replace</strong> each element in that subarray with the <strong>bitwise AND</strong> of all elements.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations required to make all elements of <code>nums</code> equal.</p>
|
||||
A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Choose <code>nums[0...1]</code>: <code>(1 AND 2) = 0</code>, so the array becomes <code>[0, 0]</code> and all elements are equal in 1 operation.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>nums</code> is <code>[5, 5, 5]</code> which already has all elements equal, so 0 operations are required.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You need to choose <strong>exactly</strong> <code>k</code> non-empty <span data-keyword="subarray-nonempty">subarrays</span> <code>nums[l..r]</code> of <code>nums</code>. Subarrays may overlap, and the exact same subarray (same <code>l</code> and <code>r</code>) <strong>can</strong> be chosen more than once.</p>
|
||||
|
||||
<p>The <strong>value</strong> of a subarray <code>nums[l..r]</code> is defined as: <code>max(nums[l..r]) - min(nums[l..r])</code>.</p>
|
||||
|
||||
<p>The <strong>total value</strong> is the sum of the <strong>values</strong> of all chosen subarrays.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> possible total value you can achieve.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal approach is:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <code>nums[0..1] = [1, 3]</code>. The maximum is 3 and the minimum is 1, giving a value of <code>3 - 1 = 2</code>.</li>
|
||||
<li>Choose <code>nums[0..2] = [1, 3, 2]</code>. The maximum is still 3 and the minimum is still 1, so the value is also <code>3 - 1 = 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Adding these gives <code>2 + 2 = 4</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,2,5,1], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">12</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal approach is:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <code>nums[0..3] = [4, 2, 5, 1]</code>. The maximum is 5 and the minimum is 1, giving a value of <code>5 - 1 = 4</code>.</li>
|
||||
<li>Choose <code>nums[0..3] = [4, 2, 5, 1]</code>. The maximum is 5 and the minimum is 1, so the value is also <code>4</code>.</li>
|
||||
<li>Choose <code>nums[2..3] = [5, 1]</code>. The maximum is 5 and the minimum is 1, so the value is again <code>4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Adding these gives <code>4 + 4 + 4 = 12</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You must select <strong>exactly</strong> <code>k</code> <strong>distinct</strong> non-empty <span data-keyword="subarray-nonempty">subarrays</span> <code>nums[l..r]</code> of <code>nums</code>. Subarrays may overlap, but the exact same subarray (same <code>l</code> and <code>r</code>) <strong>cannot</strong> be chosen more than once.</p>
|
||||
|
||||
<p>The <strong>value</strong> of a subarray <code>nums[l..r]</code> is defined as: <code>max(nums[l..r]) - min(nums[l..r])</code>.</p>
|
||||
|
||||
<p>The <strong>total value</strong> is the sum of the <strong>values</strong> of all chosen subarrays.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> possible total value you can achieve.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal approach is:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <code>nums[0..1] = [1, 3]</code>. The maximum is 3 and the minimum is 1, giving a value of <code>3 - 1 = 2</code>.</li>
|
||||
<li>Choose <code>nums[0..2] = [1, 3, 2]</code>. The maximum is still 3 and the minimum is still 1, so the value is also <code>3 - 1 = 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Adding these gives <code>2 + 2 = 4</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,2,5,1], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">12</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal approach is:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <code>nums[0..3] = [4, 2, 5, 1]</code>. The maximum is 5 and the minimum is 1, giving a value of <code>5 - 1 = 4</code>.</li>
|
||||
<li>Choose <code>nums[1..3] = [2, 5, 1]</code>. The maximum is 5 and the minimum is 1, so the value is also <code>4</code>.</li>
|
||||
<li>Choose <code>nums[2..3] = [5, 1]</code>. The maximum is 5 and the minimum is 1, so the value is again <code>4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Adding these gives <code>4 + 4 + 4 = 12</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= min(10<sup>5</sup>, n * (n + 1) / 2)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,127 @@
|
||||
<p>Table: <code>app_events</code></p>
|
||||
|
||||
<pre>
|
||||
+------------------+----------+
|
||||
| Column Name | Type |
|
||||
+------------------+----------+
|
||||
| event_id | int |
|
||||
| user_id | int |
|
||||
| event_timestamp | datetime |
|
||||
| event_type | varchar |
|
||||
| session_id | varchar |
|
||||
| event_value | int |
|
||||
+------------------+----------+
|
||||
event_id is the unique identifier for this table.
|
||||
event_type can be app_open, click, scroll, purchase, or app_close.
|
||||
session_id groups events within the same user session.
|
||||
event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to identify <strong>zombie sessions, </strong>sessions where users appear active but show abnormal behavior patterns. A session is considered a <strong>zombie session</strong> if it meets ALL the following criteria:</p>
|
||||
|
||||
<ul>
|
||||
<li>The session duration is <strong>more than</strong> <code>30</code> minutes.</li>
|
||||
<li>Has <strong>at least</strong> <code>5</code> scroll events.</li>
|
||||
<li>The <strong>click-to-scroll ratio</strong> is less than <code>0.20</code> .</li>
|
||||
<li><strong>No purchases</strong> were made during the session.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the result table ordered by</em> <code>scroll_count</code> <em>in <strong>descending</strong> order, then by</em> <code>session_id</code> <em>in <strong>ascending</strong> order</em>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong></p>
|
||||
|
||||
<p>app_events table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+----------+---------+---------------------+------------+------------+-------------+
|
||||
| event_id | user_id | event_timestamp | event_type | session_id | event_value |
|
||||
+----------+---------+---------------------+------------+------------+-------------+
|
||||
| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |
|
||||
| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |
|
||||
| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |
|
||||
| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |
|
||||
| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |
|
||||
| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |
|
||||
| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |
|
||||
| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |
|
||||
| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |
|
||||
| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |
|
||||
| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |
|
||||
| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |
|
||||
| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |
|
||||
| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |
|
||||
| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |
|
||||
| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |
|
||||
| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |
|
||||
| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |
|
||||
| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |
|
||||
| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |
|
||||
| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |
|
||||
| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |
|
||||
| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |
|
||||
| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |
|
||||
| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |
|
||||
| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |
|
||||
| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |
|
||||
| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |
|
||||
+----------+---------+---------------------+------------+------------+-------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------+---------+--------------------------+--------------+
|
||||
| session_id | user_id | session_duration_minutes | scroll_count |
|
||||
+------------+---------+--------------------------+--------------+
|
||||
| S001 | 201 | 35 | 6 |
|
||||
+------------+---------+--------------------------+--------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Session S001 (User 201)</strong>:
|
||||
|
||||
<ul>
|
||||
<li>Duration: 10:00:00 to 10:35:00 = 35 minutes (more than 30) </li>
|
||||
<li>Scroll events: 6 (at least 5) </li>
|
||||
<li>Click events: 0</li>
|
||||
<li>Click-to-scroll ratio: 0/6 = 0.00 (less than 0.20) </li>
|
||||
<li>Purchases: 0 (no purchases) </li>
|
||||
<li>S001 is a zombie session (meets all criteria)</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Session S002 (User 202)</strong>:
|
||||
<ul>
|
||||
<li>Duration: 11:00:00 to 11:20:00 = 20 minutes (less than 30) </li>
|
||||
<li>Has a purchase event </li>
|
||||
<li>S002 is not a zombie session </li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Session S003 (User 203)</strong>:
|
||||
<ul>
|
||||
<li>Duration: 12:00:00 to 13:00:00 = 60 minutes (more than 30) </li>
|
||||
<li>Scroll events: 5 (at least 5) </li>
|
||||
<li>Click events: 1</li>
|
||||
<li>Click-to-scroll ratio: 1/5 = 0.20 (not less than 0.20) </li>
|
||||
<li>Purchases: 0 (no purchases) </li>
|
||||
<li>S003 is not a zombie session (click-to-scroll ratio equals 0.20, needs to be less)</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Session S004 (User 204)</strong>:
|
||||
<ul>
|
||||
<li>Duration: 14:00:00 to 14:12:00 = 12 minutes (less than 30) </li>
|
||||
<li>Scroll events: 2 (less than 5) </li>
|
||||
<li>S004 is not a zombie session </li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>The result table is ordered by scroll_count in descending order, then by session_id in ascending order.</p>
|
||||
</div>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given an integer <code>n</code> representing <code>n</code> teams. You are asked to generate a schedule such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each team plays every other team <strong>exactly twice</strong>: once at home and once away.</li>
|
||||
<li>There is <strong>exactly one</strong> match per day; the schedule is a list of <strong>consecutive</strong> days and <code>schedule[i]</code> is the match on day <code>i</code>.</li>
|
||||
<li>No team plays on <strong>consecutive</strong> days.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return a 2D integer array <code>schedule</code>, where <code>schedule[i][0]</code> represents the home team and <code>schedule[i][1]</code> represents the away team. If multiple schedules meet the conditions, return <strong>any</strong> one of them.</p>
|
||||
|
||||
<p>If no schedule exists that meets the conditions, return an empty array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Since each team plays every other team exactly twice, a total of 6 matches need to be played: <code>[0,1],[0,2],[1,2],[1,0],[2,0],[2,1]</code>.</p>
|
||||
|
||||
<p>It's not possible to create a schedule without at least one team playing consecutive days.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[0,1],[2,3],[0,4],[1,2],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[2,0],[3,1],[4,0],[2,1],[4,3],[1,0],[3,2],[4,1],[3,0],[4,2]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Since each team plays every other team exactly twice, a total of 20 matches need to be played.</p>
|
||||
|
||||
<p>The output shows one of the schedules that meet the conditions. No team plays on consecutive days.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>You are given an integer array <code>nums</code> with <strong>distinct</strong> elements.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[l...r]</code> of <code>nums</code> is called a <strong>bowl</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>The subarray has length at least 3. That is, <code>r - l + 1 >= 3</code>.</li>
|
||||
<li>The <strong>minimum</strong> of its two ends is <strong>strictly greater</strong> than the <strong>maximum</strong> of all elements in between. That is, <code>min(nums[l], nums[r]) > max(nums[l + 1], ..., nums[r - 1])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the number of <strong>bowl</strong> subarrays in <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,3,1,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The bowl subarrays are <code>[3, 1, 4]</code> and <code>[5, 3, 1, 4]</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>[3, 1, 4]</code> is a bowl because <code>min(3, 4) = 3 > max(1) = 1</code>.</li>
|
||||
<li><code>[5, 3, 1, 4]</code> is a bowl because <code>min(5, 4) = 4 > max(3, 1) = 3</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,1,2,3,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The bowl subarrays are <code>[5, 1, 2]</code>, <code>[5, 1, 2, 3]</code> and <code>[5, 1, 2, 3, 4]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = </span>[1000000000,999999999,999999998]</p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>No subarray is a bowl.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>nums</code> consists of distinct elements.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong><span data-keyword="subsequence-array-nonempty">subsequence</span></strong> is <strong>stable</strong> if it does not contain <strong>three consecutive</strong> elements with the <strong>same</strong> parity when the subsequence is read <strong>in order</strong> (i.e., consecutive <strong>inside the subsequence</strong>).</p>
|
||||
|
||||
<p>Return the number of stable subsequences.</p>
|
||||
|
||||
<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Stable subsequences are <code>[1]</code>, <code>[3]</code>, <code>[5]</code>, <code>[1, 3]</code>, <code>[1, 5]</code>, and <code>[3, 5]</code>.</li>
|
||||
<li>Subsequence <code>[1, 3, 5]</code> is not stable because it contains three consecutive odd numbers. Thus, the answer is 6.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = </span>[2,3,4,2]</p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">14</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The only subsequence that is not stable is <code>[2, 4, 2]</code>, which contains three consecutive even numbers.</li>
|
||||
<li>All other subsequences are stable. Thus, the answer is 14.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<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,50 @@
|
||||
<p>You are given a <strong>non-negative</strong> integer <code>n</code>.</p>
|
||||
|
||||
<p>A <strong>non-negative</strong> integer is called <strong>binary-palindromic</strong> if its binary representation (written without leading zeros) reads the same forward and backward.</p>
|
||||
|
||||
<p>Return the number of integers <code><font face="monospace">k</font></code> such that <code>0 <= k <= n</code> and the binary representation of <code><font face="monospace">k</font></code> is a palindrome.</p>
|
||||
|
||||
<p><strong>Note:</strong> The number 0 is considered binary-palindromic, and its representation is <code>"0"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The integers <code>k</code> in the range <code>[0, 9]</code> whose binary representations are palindromes are:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 → "0"</code></li>
|
||||
<li><code>1 → "1"</code></li>
|
||||
<li><code>3 → "11"</code></li>
|
||||
<li><code>5 → "101"</code></li>
|
||||
<li><code>7 → "111"</code></li>
|
||||
<li><code>9 → "1001"</code></li>
|
||||
</ul>
|
||||
|
||||
<p>All other values in <code>[0, 9]</code> have non-palindromic binary forms. Therefore, the count is 6.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Since <code>"0"</code> is a palindrome, the count is 1.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>15</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>positive</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Choose at most <code>k</code> elements from <code>nums</code> so that their sum is maximized. However, the chosen numbers must be <strong>distinct</strong>.</p>
|
||||
|
||||
<p>Return an array containing the chosen numbers in <strong>strictly descending</strong> order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [84,93,100,77,90], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[100,93,90]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The maximum sum is 283, which is attained by choosing 93, 100 and 90. We rearrange them in strictly descending order as <code>[100, 93, 90]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [84,93,100,77,93], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[100,93,84]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The maximum sum is 277, which is attained by choosing 84, 93 and 100. We rearrange them in strictly descending order as <code>[100, 93, <span class="example-io">84</span>]</code>. We cannot choose 93, 100 and 93 because the chosen numbers must be distinct.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,2,2,2], k = 6</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The maximum sum is 3, which is attained by choosing 1 and 2. We rearrange them in strictly descending order as <code>[2, 1]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given a string <code>s</code> consisting only of lowercase English letters.</p>
|
||||
|
||||
<p>You can perform the following operation any number of times (including zero):</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p>Choose any character <code>c</code> in the string and replace <strong>every</strong> occurrence of <code>c</code> with the <strong>next</strong> lowercase letter in the English alphabet.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations required to transform <code>s</code> into a string consisting of <strong>only</strong> <code>'a'</code> characters.</p>
|
||||
|
||||
<p><strong>Note: </strong>Consider the alphabet as circular, thus <code>'a'</code> comes after <code>'z'</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "yz"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Change <code>'y'</code> to <code>'z'</code> to get <code>"zz"</code>.</li>
|
||||
<li>Change <code>'z'</code> to <code>'a'</code> to get <code>"aa"</code>.</li>
|
||||
<li>Thus, the answer is 2.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "a"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The string <code>"a"</code> only consists of <code>'a'</code> characters. Thus, the answer is 0.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user