mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 07:51:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<p>You are given a string <code><font face="monospace">caption</font></code> representing the caption for a video.</p>
|
||||
|
||||
<p>The following actions must be performed <strong>in order</strong> to generate a <strong>valid tag</strong> for the video:</p>
|
||||
|
||||
<ol>
|
||||
<li>
|
||||
<p><strong>Combine all words</strong> in the string into a single <em>camelCase string</em> prefixed with <code>'#'</code>. A <em>camelCase string</em> is one where the first letter of all words <em>except</em> the first one is capitalized. All characters after the first character in <strong>each</strong> word must be lowercase.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p><b>Remove</b> all characters that are not an English letter, <strong>except</strong> the first <code>'#'</code>.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p><strong>Truncate</strong> the result to a maximum of 100 characters.</p>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<p>Return the <strong>tag</strong> after performing the actions on <code>caption</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">caption = "Leetcode daily streak achieved"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"#leetcodeDailyStreakAchieved"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The first letter for all words except <code>"leetcode"</code> should be capitalized.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">caption = "can I Go There"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"#canIGoThere"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The first letter for all words except <code>"can"</code> should be capitalized.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">caption = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"#hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Since the first word has length 101, we need to truncate the last two letters from the word.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= caption.length <= 150</code></li>
|
||||
<li><code>caption</code> consists only of English letters and <code>' '</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a stock in dollars on the <code>i<sup>th</sup></code> day, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are allowed to make at most <code>k</code> transactions, where each transaction can be either of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p><strong>Normal transaction</strong>: Buy on day <code>i</code>, then sell on a later day <code>j</code> where <code>i < j</code>. You profit <code>prices[j] - prices[i]</code>.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p><strong>Short selling transaction</strong>: Sell on day <code>i</code>, then buy back on a later day <code>j</code> where <code>i < j</code>. You profit <code>prices[i] - prices[j]</code>.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you must complete each transaction before starting another. Additionally, you can't buy or sell on the same day you are selling or buying back as part of a previous transaction.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> total profit you can earn by making <strong>at most</strong> <code>k</code> transactions.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">prices = [1,7,9,8,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">14</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
We can make $14 of profit through 2 transactions:
|
||||
|
||||
<ul>
|
||||
<li>A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9.</li>
|
||||
<li>A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">prices = [12,16,19,19,8,1,19,13,9], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">36</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
We can make $36 of profit through 3 transactions:
|
||||
|
||||
<ul>
|
||||
<li>A normal transaction: buy the stock on day 0 for $12 then sell it on day 2 for $19.</li>
|
||||
<li>A short selling transaction: sell the stock on day 3 for $19 then buy back on day 4 for $8.</li>
|
||||
<li>A normal transaction: buy the stock on day 5 for $1 then sell it on day 6 for $19.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= prices.length <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= prices[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= prices.length / 2</code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given an integer array <code>nums</code> having length <code>n</code> and a 2D integer array <code>queries</code> where <code>queries[i] = [idx, val]</code>.</p>
|
||||
|
||||
<p>For each query:</p>
|
||||
|
||||
<ol>
|
||||
<li>Update <code>nums[idx] = val</code>.</li>
|
||||
<li>Choose an integer <code>k</code> with <code>1 <= k < n</code> to split the array into the non-empty prefix <code>nums[0..k-1]</code> and suffix <code>nums[k..n-1]</code> such that the sum of the counts of <strong>distinct</strong> <span data-keyword="prime-number">prime</span> values in each part is <strong>maximum</strong>.</li>
|
||||
</ol>
|
||||
|
||||
<p><strong data-end="513" data-start="504">Note:</strong> The changes made to the array in one query persist into the next query.</p>
|
||||
|
||||
<p>Return an array containing the result for each query, in the order they are given.</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,1,3,1,2], queries = [[1,2],[3,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,4]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Initially <code>nums = [2, 1, 3, 1, 2]</code>.</li>
|
||||
<li>After 1<sup>st</sup> query, <code>nums = [2, 2, 3, 1, 2]</code>. Split <code>nums</code> into <code>[2]</code> and <code>[2, 3, 1, 2]</code>. <code>[2]</code> consists of 1 distinct prime and <code>[2, 3, 1, 2]</code> consists of 2 distinct primes. Hence, the answer for this query is <code>1 + 2 = 3</code>.</li>
|
||||
<li>After 2<sup>nd</sup> query, <code>nums = [2, 2, 3, 3, 2]</code>. Split <code>nums</code> into <code>[2, 2, 3]</code> and <code>[3, 2]</code> with an answer of <code>2 + 2 = 4</code>.</li>
|
||||
<li>The output is <code>[3, 4]</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 = [2,1,4], queries = [[0,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Initially <code>nums = [2, 1, 4]</code>.</li>
|
||||
<li>After 1<sup>st</sup> query, <code>nums = [1, 1, 4]</code>. There are no prime numbers in <code>nums</code>, hence the answer for this query is 0.</li>
|
||||
<li>The output is <code>[0]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= queries[i][0] < nums.length</code></li>
|
||||
<li><code>1 <= queries[i][1] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>m</code>.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> product of the first and last elements of any <strong><span data-keyword="subsequence-array">subsequence</span></strong> of <code>nums</code> of size <code>m</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,-9,2,3,-2,-3,1], m = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">81</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subsequence <code>[-9]</code> has the largest product of the first and last elements: <code>-9 * -9 = 81</code>. Therefore, the answer is 81.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,-5,5,6,-4], m = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">20</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subsequence <code>[-5, 6, -4]</code> has the largest product of the first and last elements.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,-1,2,-6,5,2,-5,7], m = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">35</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subsequence <code>[5, 7]</code> has the largest product of the first and last elements.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,81 @@
|
||||
<p>You are given an <code>m x n</code> integer matrix <code>grid</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>For every contiguous <code>k x k</code> <strong>submatrix</strong> of <code>grid</code>, compute the <strong>minimum absolute</strong> difference between any two <strong>distinct</strong> values within that <strong>submatrix</strong>.</p>
|
||||
|
||||
<p>Return a 2D array <code>ans</code> of size <code>(m - k + 1) x (n - k + 1)</code>, where <code>ans[i][j]</code> is the minimum absolute difference in the submatrix whose top-left corner is <code>(i, j)</code> in <code>grid</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>: If all elements in the submatrix have the same value, the answer will be 0.</p>
|
||||
A submatrix <code>(x1, y1, x2, y2)</code> is a matrix that is formed by choosing all cells <code>matrix[x][y]</code> where <code>x1 <= x <= x2</code> and <code>y1 <= y <= y2</code>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[1,8],[3,-2]], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[2]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>There is only one possible <code>k x k</code> submatrix: <code><span class="example-io">[[1, 8], [3, -2]]</span></code><span class="example-io">.</span></li>
|
||||
<li>Distinct values in the submatrix are<span class="example-io"> <code>[1, 8, 3, -2]</code>.</span></li>
|
||||
<li>The minimum absolute difference in the submatrix is <code>|1 - 3| = 2</code>. Thus, the answer is <code>[[2]]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[3,-1]], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[0,0]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Both <code>k x k</code> submatrix has only one distinct element.</li>
|
||||
<li>Thus, the answer is <code>[[0, 0]]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">grid = [[1,-2,3],[2,3,5]], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[[1,2]]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>There are two possible <code>k × k</code> submatrix:
|
||||
|
||||
<ul>
|
||||
<li>Starting at <code>(0, 0)</code>: <code>[[1, -2], [2, 3]]</code>.
|
||||
|
||||
<ul>
|
||||
<li>Distinct values in the submatrix are <code>[1, -2, 2, 3]</code>.</li>
|
||||
<li>The minimum absolute difference in the submatrix is <code>|1 - 2| = 1</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Starting at <code>(0, 1)</code>: <code>[[-2, 3], [3, 5]]</code>.
|
||||
<ul>
|
||||
<li>Distinct values in the submatrix are <code>[-2, 3, 5]</code>.</li>
|
||||
<li>The minimum absolute difference in the submatrix is <code>|3 - 5| = 2</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Thus, the answer is <code>[[1, 2]]</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m == grid.length <= 30</code></li>
|
||||
<li><code>1 <= n == grid[i].length <= 30</code></li>
|
||||
<li><code>-10<sup>5</sup> <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= min(m, n)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,111 @@
|
||||
<p>You are given two strings, <code>word1</code> and <code>word2</code>, of equal length. You need to transform <code>word1</code> into <code>word2</code>.</p>
|
||||
|
||||
<p>For this, divide <code>word1</code> into one or more <strong>contiguous <span data-keyword="substring-nonempty">substrings</span></strong>. For each substring <code>substr</code> you can perform the following operations:</p>
|
||||
|
||||
<ol>
|
||||
<li>
|
||||
<p><strong>Replace:</strong> Replace the character at any one index of <code>substr</code> with another lowercase English letter.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p><strong>Swap:</strong> Swap any two characters in <code>substr</code>.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p><strong>Reverse Substring:</strong> Reverse <code>substr</code>.</p>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<p>Each of these counts as <strong>one</strong> operation and each character of each substring can be used in each type of operation at most once (i.e. no single index may be involved in more than one replace, one swap, or one reverse).</p>
|
||||
|
||||
<p>Return the <strong>minimum number of operations</strong> required to transform <code>word1</code> into <code>word2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "abcdf", word2 = "dacbe"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Divide <code>word1</code> into <code>"ab"</code>, <code>"c"</code>, and <code>"df"</code>. The operations are:</p>
|
||||
|
||||
<ul>
|
||||
<li>For the substring <code>"ab"</code>,
|
||||
|
||||
<ul>
|
||||
<li>Perform operation of type 3 on <code>"ab" -> "ba"</code>.</li>
|
||||
<li>Perform operation of type 1 on <code>"ba" -> "da"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For the substring <code>"c"</code> do no operations.</li>
|
||||
<li>For the substring <code>"df"</code>,
|
||||
<ul>
|
||||
<li>Perform operation of type 1 on <code>"df" -> "bf"</code>.</li>
|
||||
<li>Perform operation of type 1 on <code>"bf" -> "be"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "abceded", word2 = "baecfef"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Divide <code>word1</code> into <code>"ab"</code>, <code>"ce"</code>, and <code>"ded"</code>. The operations are:</p>
|
||||
|
||||
<ul>
|
||||
<li>For the substring <code>"ab"</code>,
|
||||
|
||||
<ul>
|
||||
<li>Perform operation of type 2 on <code>"ab" -> "ba"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For the substring <code>"ce"</code>,
|
||||
<ul>
|
||||
<li>Perform operation of type 2 on <code>"ce" -> "ec"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For the substring <code>"ded"</code>,
|
||||
<ul>
|
||||
<li>Perform operation of type 1 on <code>"ded" -> "fed"</code>.</li>
|
||||
<li>Perform operation of type 1 on <code>"fed" -> "fef"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">word1 = "abcdef", word2 = "fedabc"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Divide <code>word1</code> into <code>"abcdef"</code>. The operations are:</p>
|
||||
|
||||
<ul>
|
||||
<li>For the substring <code>"abcdef"</code>,
|
||||
|
||||
<ul>
|
||||
<li>Perform operation of type 3 on <code>"abcdef" -> "fedcba"</code>.</li>
|
||||
<li>Perform operation of type 2 on <code>"fedcba" -> "fedabc"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length == word2.length <= 100</code></li>
|
||||
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,143 @@
|
||||
<p>Table: <code>sales</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| sale_id | int |
|
||||
| product_id | int |
|
||||
| sale_date | date |
|
||||
| quantity | int |
|
||||
| price | decimal |
|
||||
+---------------+---------+
|
||||
sale_id is the unique identifier for this table.
|
||||
Each row contains information about a product sale including the product_id, date of sale, quantity sold, and price per unit.
|
||||
</pre>
|
||||
|
||||
<p>Table: <code>products</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
| category | varchar |
|
||||
+---------------+---------+
|
||||
product_id is the unique identifier for this table.
|
||||
Each row contains information about a product including its name and category.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to find the most popular product category for each season. The seasons are defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Winter</strong>: December, January, February</li>
|
||||
<li><strong>Spring</strong>: March, April, May</li>
|
||||
<li><strong>Summer</strong>: June, July, August</li>
|
||||
<li><strong>Fall</strong>: September, October, November</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>popularity</strong> of a <strong>category</strong> is determined by the <strong>total quantity sold</strong> in that <strong>season</strong>. If there is a <strong>tie</strong>, select the category with the highest <strong>total revenue</strong> (<code>quantity × price</code>).</p>
|
||||
|
||||
<p>Return <em>the result table ordered by season 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>sales table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+---------+------------+------------+----------+-------+
|
||||
| sale_id | product_id | sale_date | quantity | price |
|
||||
+---------+------------+------------+----------+-------+
|
||||
| 1 | 1 | 2023-01-15 | 5 | 10.00 |
|
||||
| 2 | 2 | 2023-01-20 | 4 | 15.00 |
|
||||
| 3 | 3 | 2023-03-10 | 3 | 18.00 |
|
||||
| 4 | 4 | 2023-04-05 | 1 | 20.00 |
|
||||
| 5 | 1 | 2023-05-20 | 2 | 10.00 |
|
||||
| 6 | 2 | 2023-06-12 | 4 | 15.00 |
|
||||
| 7 | 5 | 2023-06-15 | 5 | 12.00 |
|
||||
| 8 | 3 | 2023-07-24 | 2 | 18.00 |
|
||||
| 9 | 4 | 2023-08-01 | 5 | 20.00 |
|
||||
| 10 | 5 | 2023-09-03 | 3 | 12.00 |
|
||||
| 11 | 1 | 2023-09-25 | 6 | 10.00 |
|
||||
| 12 | 2 | 2023-11-10 | 4 | 15.00 |
|
||||
| 13 | 3 | 2023-12-05 | 6 | 18.00 |
|
||||
| 14 | 4 | 2023-12-22 | 3 | 20.00 |
|
||||
| 15 | 5 | 2024-02-14 | 2 | 12.00 |
|
||||
+---------+------------+------------+----------+-------+
|
||||
</pre>
|
||||
|
||||
<p>products table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------+-----------------+----------+
|
||||
| product_id | product_name | category |
|
||||
+------------+-----------------+----------+
|
||||
| 1 | Warm Jacket | Apparel |
|
||||
| 2 | Designer Jeans | Apparel |
|
||||
| 3 | Cutting Board | Kitchen |
|
||||
| 4 | Smart Speaker | Tech |
|
||||
| 5 | Yoga Mat | Fitness |
|
||||
+------------+-----------------+----------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+---------+----------+----------------+---------------+
|
||||
| season | category | total_quantity | total_revenue |
|
||||
+---------+----------+----------------+---------------+
|
||||
| Fall | Apparel | 10 | 120.00 |
|
||||
| Spring | Kitchen | 3 | 54.00 |
|
||||
| Summer | Tech | 5 | 100.00 |
|
||||
| Winter | Apparel | 9 | 110.00 |
|
||||
+---------+----------+----------------+---------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Fall (Sep, Oct, Nov):</strong>
|
||||
|
||||
<ul>
|
||||
<li>Apparel: 10 items sold (6 Jackets in Sep, 4 Jeans in Nov), revenue $120.00 (6×$10.00 + 4×$15.00)</li>
|
||||
<li>Fitness: 3 Yoga Mats sold in Sep, revenue $36.00</li>
|
||||
<li>Most popular: Apparel with highest total quantity (10)</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Spring (Mar, Apr, May):</strong>
|
||||
<ul>
|
||||
<li>Kitchen: 3 Cutting Boards sold in Mar, revenue $54.00</li>
|
||||
<li>Tech: 1 Smart Speaker sold in Apr, revenue $20.00</li>
|
||||
<li>Apparel: 2 Warm Jackets sold in May, revenue $20.00</li>
|
||||
<li>Most popular: Kitchen with highest total quantity (3) and highest revenue ($54.00)</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Summer (Jun, Jul, Aug):</strong>
|
||||
<ul>
|
||||
<li>Apparel: 4 Designer Jeans sold in Jun, revenue $60.00</li>
|
||||
<li>Fitness: 5 Yoga Mats sold in Jun, revenue $60.00</li>
|
||||
<li>Kitchen: 2 Cutting Boards sold in Jul, revenue $36.00</li>
|
||||
<li>Tech: 5 Smart Speakers sold in Aug, revenue $100.00</li>
|
||||
<li>Most popular: Tech and Fitness both have 5 items, but Tech has higher revenue ($100.00 vs $60.00)</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Winter (Dec, Jan, Feb):</strong>
|
||||
<ul>
|
||||
<li>Apparel: 9 items sold (5 Jackets in Jan, 4 Jeans in Jan), revenue $110.00</li>
|
||||
<li>Kitchen: 6 Cutting Boards sold in Dec, revenue $108.00</li>
|
||||
<li>Tech: 3 Smart Speakers sold in Dec, revenue $60.00</li>
|
||||
<li>Fitness: 2 Yoga Mats sold in Feb, revenue $24.00</li>
|
||||
<li>Most popular: Apparel with highest total quantity (9) and highest revenue ($110.00)</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>The result table is ordered by season in ascending order.</p>
|
||||
</div>
|
@@ -0,0 +1,131 @@
|
||||
<p>Table: <code>patients</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| patient_id | int |
|
||||
| patient_name| varchar |
|
||||
| age | int |
|
||||
+-------------+---------+
|
||||
patient_id is the unique identifier for this table.
|
||||
Each row contains information about a patient.
|
||||
</pre>
|
||||
|
||||
<p>Table: <code>covid_tests</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| test_id | int |
|
||||
| patient_id | int |
|
||||
| test_date | date |
|
||||
| result | varchar |
|
||||
+-------------+---------+
|
||||
test_id is the unique identifier for this table.
|
||||
Each row represents a COVID test result. The result can be Positive, Negative, or Inconclusive.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to find patients who have <strong>recovered from COVID</strong> - patients who tested positive but later tested negative.</p>
|
||||
|
||||
<ul>
|
||||
<li>A patient is considered recovered if they have <strong>at least one</strong> <strong>Positive</strong> test followed by at least one <strong>Negative</strong> test on a <strong>later date</strong></li>
|
||||
<li>Calculate the <strong>recovery time</strong> in days as the <strong>difference</strong> between the <strong>first positive test</strong> and the <strong>first negative test</strong> after that <strong>positive test</strong></li>
|
||||
<li><strong>Only include</strong> patients who have both positive and negative test results</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the result table ordered by </em><code>recovery_time</code><em> in <strong>ascending</strong> order, then by </em><code>patient_name</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>patients table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------+--------------+-----+
|
||||
| patient_id | patient_name | age |
|
||||
+------------+--------------+-----+
|
||||
| 1 | Alice Smith | 28 |
|
||||
| 2 | Bob Johnson | 35 |
|
||||
| 3 | Carol Davis | 42 |
|
||||
| 4 | David Wilson | 31 |
|
||||
| 5 | Emma Brown | 29 |
|
||||
+------------+--------------+-----+
|
||||
</pre>
|
||||
|
||||
<p>covid_tests table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+---------+------------+------------+--------------+
|
||||
| test_id | patient_id | test_date | result |
|
||||
+---------+------------+------------+--------------+
|
||||
| 1 | 1 | 2023-01-15 | Positive |
|
||||
| 2 | 1 | 2023-01-25 | Negative |
|
||||
| 3 | 2 | 2023-02-01 | Positive |
|
||||
| 4 | 2 | 2023-02-05 | Inconclusive |
|
||||
| 5 | 2 | 2023-02-12 | Negative |
|
||||
| 6 | 3 | 2023-01-20 | Negative |
|
||||
| 7 | 3 | 2023-02-10 | Positive |
|
||||
| 8 | 3 | 2023-02-20 | Negative |
|
||||
| 9 | 4 | 2023-01-10 | Positive |
|
||||
| 10 | 4 | 2023-01-18 | Positive |
|
||||
| 11 | 5 | 2023-02-15 | Negative |
|
||||
| 12 | 5 | 2023-02-20 | Negative |
|
||||
+---------+------------+------------+--------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------+--------------+-----+---------------+
|
||||
| patient_id | patient_name | age | recovery_time |
|
||||
+------------+--------------+-----+---------------+
|
||||
| 1 | Alice Smith | 28 | 10 |
|
||||
| 3 | Carol Davis | 42 | 10 |
|
||||
| 2 | Bob Johnson | 35 | 11 |
|
||||
+------------+--------------+-----+---------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Alice Smith (patient_id = 1):</strong>
|
||||
|
||||
<ul>
|
||||
<li>First positive test: 2023-01-15</li>
|
||||
<li>First negative test after positive: 2023-01-25</li>
|
||||
<li>Recovery time: 25 - 15 = 10 days</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Bob Johnson (patient_id = 2):</strong>
|
||||
<ul>
|
||||
<li>First positive test: 2023-02-01</li>
|
||||
<li>Inconclusive test on 2023-02-05 (ignored for recovery calculation)</li>
|
||||
<li>First negative test after positive: 2023-02-12</li>
|
||||
<li>Recovery time: 12 - 1 = 11 days</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Carol Davis (patient_id = 3):</strong>
|
||||
<ul>
|
||||
<li>Had negative test on 2023-01-20 (before positive test)</li>
|
||||
<li>First positive test: 2023-02-10</li>
|
||||
<li>First negative test after positive: 2023-02-20</li>
|
||||
<li>Recovery time: 20 - 10 = 10 days</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Patients not included:</strong>
|
||||
<ul>
|
||||
<li>David Wilson (patient_id = 4): Only has positive tests, no negative test after positive</li>
|
||||
<li>Emma Brown (patient_id = 5): Only has negative tests, never tested positive</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Output table is ordered by recovery_time in ascending order, and then by patient_name in ascending order.</p>
|
||||
</div>
|
@@ -0,0 +1,137 @@
|
||||
<p>Table: <code>employees</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| employee_id | int |
|
||||
| name | varchar |
|
||||
+-------------+---------+
|
||||
employee_id is the unique identifier for this table.
|
||||
Each row contains information about an employee.
|
||||
</pre>
|
||||
|
||||
<p>Table: <code>performance_reviews</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| review_id | int |
|
||||
| employee_id | int |
|
||||
| review_date | date |
|
||||
| rating | int |
|
||||
+-------------+------+
|
||||
review_id is the unique identifier for this table.
|
||||
Each row represents a performance review for an employee. The rating is on a scale of 1-5 where 5 is excellent and 1 is poor.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to find employees who have consistently improved their performance over <strong>their last three reviews</strong>.</p>
|
||||
|
||||
<ul>
|
||||
<li>An employee must have <strong>at least </strong><code>3</code><strong> review</strong> to be considered</li>
|
||||
<li>The employee's <strong>last </strong><code>3</code><strong> reviews</strong> must show <strong>strictly increasing ratings</strong> (each review better than the previous)</li>
|
||||
<li>Use the most recent <code>3</code> reviews based on <code>review_date</code> for each employee</li>
|
||||
<li>Calculate the <strong>improvement score</strong> as the difference between the latest rating and the earliest rating among the last <code>3</code> reviews</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the result table ordered by <strong>improvement score</strong> in <strong>descending</strong> order, then by <strong>name</strong> 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>employees table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+-------------+----------------+
|
||||
| employee_id | name |
|
||||
+-------------+----------------+
|
||||
| 1 | Alice Johnson |
|
||||
| 2 | Bob Smith |
|
||||
| 3 | Carol Davis |
|
||||
| 4 | David Wilson |
|
||||
| 5 | Emma Brown |
|
||||
+-------------+----------------+
|
||||
</pre>
|
||||
|
||||
<p>performance_reviews table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+-----------+-------------+-------------+--------+
|
||||
| review_id | employee_id | review_date | rating |
|
||||
+-----------+-------------+-------------+--------+
|
||||
| 1 | 1 | 2023-01-15 | 2 |
|
||||
| 2 | 1 | 2023-04-15 | 3 |
|
||||
| 3 | 1 | 2023-07-15 | 4 |
|
||||
| 4 | 1 | 2023-10-15 | 5 |
|
||||
| 5 | 2 | 2023-02-01 | 3 |
|
||||
| 6 | 2 | 2023-05-01 | 2 |
|
||||
| 7 | 2 | 2023-08-01 | 4 |
|
||||
| 8 | 2 | 2023-11-01 | 5 |
|
||||
| 9 | 3 | 2023-03-10 | 1 |
|
||||
| 10 | 3 | 2023-06-10 | 2 |
|
||||
| 11 | 3 | 2023-09-10 | 3 |
|
||||
| 12 | 3 | 2023-12-10 | 4 |
|
||||
| 13 | 4 | 2023-01-20 | 4 |
|
||||
| 14 | 4 | 2023-04-20 | 4 |
|
||||
| 15 | 4 | 2023-07-20 | 4 |
|
||||
| 16 | 5 | 2023-02-15 | 3 |
|
||||
| 17 | 5 | 2023-05-15 | 2 |
|
||||
+-----------+-------------+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+-------------+----------------+-------------------+
|
||||
| employee_id | name | improvement_score |
|
||||
+-------------+----------------+-------------------+
|
||||
| 2 | Bob Smith | 3 |
|
||||
| 1 | Alice Johnson | 2 |
|
||||
| 3 | Carol Davis | 2 |
|
||||
+-------------+----------------+-------------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Alice Johnson (employee_id = 1):</strong>
|
||||
|
||||
<ul>
|
||||
<li>Has 4 reviews with ratings: 2, 3, 4, 5</li>
|
||||
<li>Last 3 reviews (by date): 2023-04-15 (3), 2023-07-15 (4), 2023-10-15 (5)</li>
|
||||
<li>Ratings are strictly increasing: 3 → 4 → 5</li>
|
||||
<li>Improvement score: 5 - 3 = 2</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Carol Davis (employee_id = 3):</strong>
|
||||
<ul>
|
||||
<li>Has 4 reviews with ratings: 1, 2, 3, 4</li>
|
||||
<li>Last 3 reviews (by date): 2023-06-10 (2), 2023-09-10 (3), 2023-12-10 (4)</li>
|
||||
<li>Ratings are strictly increasing: 2 → 3 → 4</li>
|
||||
<li>Improvement score: 4 - 2 = 2</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Bob Smith (employee_id = 2):</strong>
|
||||
<ul>
|
||||
<li>Has 4 reviews with ratings: 3, 2, 4, 5</li>
|
||||
<li>Last 3 reviews (by date): 2023-05-01 (2), 2023-08-01 (4), 2023-11-01 (5)</li>
|
||||
<li>Ratings are strictly increasing: 2 → 4 → 5</li>
|
||||
<li>Improvement score: 5 - 2 = 3</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Employees not included:</strong>
|
||||
<ul>
|
||||
<li>David Wilson (employee_id = 4): Last 3 reviews are all 4 (no improvement)</li>
|
||||
<li>Emma Brown (employee_id = 5): Only has 2 reviews (needs at least 3)</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>The output table is ordered by improvement_score in descending order, then by name in ascending order.</p>
|
||||
</div>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>nums</code> of size <code>n</code> containing only <code>1</code> and <code>-1</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform the following operation at most <code>k</code> times:</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p>Choose an index <code>i</code> (<code>0 <= i < n - 1</code>), and <strong>multiply</strong> both <code>nums[i]</code> and <code>nums[i + 1]</code> by <code>-1</code>.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you can choose the same index <code data-end="459" data-start="456">i</code> more than once in <strong>different</strong> operations.</p>
|
||||
|
||||
<p>Return <code>true</code> if it is possible to make all elements of the array <strong>equal</strong> after at most <code>k</code> operations, and <code>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 = [1,-1,1,-1,1], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can make all elements in the array equal in 2 operations as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose index <code>i = 1</code>, and multiply both <code>nums[1]</code> and <code>nums[2]</code> by -1. Now <code>nums = [1,1,-1,-1,1]</code>.</li>
|
||||
<li>Choose index <code>i = 2</code>, and multiply both <code>nums[2]</code> and <code>nums[3]</code> by -1. Now <code>nums = [1,1,1,1,1]</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 = [-1,-1,-1,1,1,1], k = 5</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>It is not possible to make all array elements equal in at most 5 operations.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> is either -1 or 1.</li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,100 @@
|
||||
<p>You are given an undirected tree rooted at node 0 with <code>n</code> nodes numbered from 0 to <code>n - 1</code>. Each node <code>i</code> has an integer value <code>vals[i]</code>, and its parent is given by <code>par[i]</code>.</p>
|
||||
|
||||
<p>A <strong>subset</strong> of nodes within the <strong>subtree</strong> of a node is called <strong>good</strong> if every digit from 0 to 9 appears <strong>at most</strong> once in the decimal representation of the values of the selected nodes.</p>
|
||||
|
||||
<p>The <strong>score</strong> of a good subset is the sum of the values of its nodes.</p>
|
||||
|
||||
<p>Define an array <code>maxScore</code> of length <code>n</code>, where <code>maxScore[u]</code> represents the <strong>maximum</strong> possible sum of values of a good subset of nodes that belong to the subtree rooted at node <code>u</code>, including <code>u</code> itself and all its descendants.</p>
|
||||
|
||||
<p>Return the sum of all values in <code>maxScore</code>.</p>
|
||||
|
||||
<p>Since the answer may be 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">vals = [2,3], par = [-1,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">8</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/screenshot-2025-04-29-at-150754.png" style="height: 84px; width: 180px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>The subtree rooted at node 0 includes nodes <code>{0, 1}</code>. The subset <code>{2, 3}</code> is<i> </i>good as the digits 2 and 3 appear only once. The score of this subset is <code>2 + 3 = 5</code>.</li>
|
||||
<li>The subtree rooted at node 1 includes only node <code>{1}</code>. The subset <code>{3}</code> is<i> </i>good. The score of this subset is 3.</li>
|
||||
<li>The <code>maxScore</code> array is <code>[5, 3]</code>, and the sum of all values in <code>maxScore</code> is <code>5 + 3 = 8</code>. Thus, the answer is 8.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">vals = [1,5,2], par = [-1,0,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">15</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/screenshot-2025-04-29-at-151408.png" style="width: 205px; height: 140px;" /></strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The subtree rooted at node 0 includes nodes <code>{0, 1, 2}</code>. The subset <code>{1, 5, 2}</code> is<i> </i>good as the digits 1, 5 and 2 appear only once. The score of this subset is <code>1 + 5 + 2 = 8</code>.</li>
|
||||
<li>The subtree rooted at node 1 includes only node <code>{1}</code>. The subset <code>{5}</code> is<i> </i>good. The score of this subset is 5.</li>
|
||||
<li>The subtree rooted at node 2 includes only node <code>{2}</code>. The subset <code>{2}</code> is<i> </i>good. The score of this subset is 2.</li>
|
||||
<li>The <code>maxScore</code> array is <code>[8, 5, 2]</code>, and the sum of all values in <code>maxScore</code> is <code>8 + 5 + 2 = 15</code>. Thus, the answer is 15.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">vals = [34,1,2], par = [-1,0,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">42</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/screenshot-2025-04-29-at-151747.png" style="height: 80px; width: 256px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>The subtree rooted at node 0 includes nodes <code>{0, 1, 2}</code>. The subset <code>{34, 1, 2}</code> is<i> </i>good as the digits 3, 4, 1 and 2 appear only once. The score of this subset is <code>34 + 1 + 2 = 37</code>.</li>
|
||||
<li>The subtree rooted at node 1 includes node <code>{1, 2}</code>. The subset <code>{1, 2}</code> is<i> </i>good as the digits 1 and 2 appear only once. The score of this subset is <code>1 + 2 = 3</code>.</li>
|
||||
<li>The subtree rooted at node 2 includes only node <code>{2}</code>. The subset <code>{2}</code> is<i> </i>good. The score of this subset is 2.</li>
|
||||
<li>The <code>maxScore</code> array is <code>[37, 3, 2]</code>, and the sum of all values in <code>maxScore</code> is <code>37 + 3 + 2 = 42</code>. Thus, the answer is 42.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">vals = [3,22,5], par = [-1,0,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">18</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The subtree rooted at node 0 includes nodes <code>{0, 1, 2}</code>. The subset <code>{3, 22, 5}</code> is<i> </i>not good, as digit 2 appears twice. Therefore, the subset <code>{3, 5}</code> is valid. The score of this subset is <code>3 + 5 = 8</code>.</li>
|
||||
<li>The subtree rooted at node 1 includes nodes <code>{1, 2}</code>. The subset <code>{22, 5}</code> is<i> </i>not good, as digit 2 appears twice. Therefore, the subset <code>{5}</code> is valid. The score of this subset is 5.</li>
|
||||
<li>The subtree rooted at node 2 includes <code>{2}</code>. The subset <code>{5}</code> is<i> </i>good. The score of this subset is 5.</li>
|
||||
<li>The <code>maxScore</code> array is <code>[8, 5, 5]</code>, and the sum of all values in <code>maxScore</code> is <code>8 + 5 + 5 = 18</code>. Thus, the answer is 18.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == vals.length <= 500</code></li>
|
||||
<li><code>1 <= vals[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>par.length == n</code></li>
|
||||
<li><code>par[0] == -1</code></li>
|
||||
<li><code>0 <= par[i] < n</code> for <code>i</code> in <code>[1, n - 1]</code></li>
|
||||
<li>The input is generated such that the parent array <code>par</code> represents a valid tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,70 @@
|
||||
<p>You are given an array of positive integers <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You may perform at most <code>k</code> operations. In each operation, you can choose one element in the array and <strong>double</strong> its value. Each element can be doubled <strong>at most</strong> once.</p>
|
||||
|
||||
<p>The <strong>score</strong> of a contiguous <strong><span data-keyword="subarray">subarray</span></strong> is defined as the <strong>product</strong> of its length and the <em>greatest common divisor (GCD)</em> of all its elements.</p>
|
||||
|
||||
<p>Your task is to return the <strong>maximum</strong> <strong>score</strong> that can be achieved by selecting a contiguous subarray from the modified array.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>greatest common divisor (GCD)</strong> of an array is the largest integer that evenly divides all the array elements.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,4], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">8</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Double <code>nums[0]</code> to 4 using one operation. The modified array becomes <code>[4, 4]</code>.</li>
|
||||
<li>The GCD of the subarray <code>[4, 4]</code> is 4, and the length is 2.</li>
|
||||
<li>Thus, the maximum possible score is <code>2 × 4 = 8</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 = [3,5,7], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">14</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Double <code>nums[2]</code> to 14 using one operation. The modified array becomes <code>[3, 5, 14]</code>.</li>
|
||||
<li>The GCD of the subarray <code>[14]</code> is 14, and the length is 1.</li>
|
||||
<li>Thus, the maximum possible score is <code>1 × 14 = 14</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">15</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The subarray <code>[5, 5, 5]</code> has a GCD of 5, and its length is 3.</li>
|
||||
<li>Since doubling any element doesn't improve the score, the maximum score is <code>3 × 5 = 15</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 1500</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,132 @@
|
||||
<p>Table: <code>library_books</code></p>
|
||||
|
||||
<pre>
|
||||
+------------------+---------+
|
||||
| Column Name | Type |
|
||||
+------------------+---------+
|
||||
| book_id | int |
|
||||
| title | varchar |
|
||||
| author | varchar |
|
||||
| genre | varchar |
|
||||
| publication_year | int |
|
||||
| total_copies | int |
|
||||
+------------------+---------+
|
||||
book_id is the unique identifier for this table.
|
||||
Each row contains information about a book in the library, including the total number of copies owned by the library.
|
||||
</pre>
|
||||
|
||||
<p>Table: <code>borrowing_records</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| record_id | int |
|
||||
| book_id | int |
|
||||
| borrower_name | varchar |
|
||||
| borrow_date | date |
|
||||
| return_date | date |
|
||||
+---------------+---------+
|
||||
record_id is the unique identifier for this table.
|
||||
Each row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to find <strong>all books</strong> that are <strong>currently borrowed (not returned)</strong> and have <strong>zero copies available</strong> in the library.</p>
|
||||
|
||||
<ul>
|
||||
<li>A book is considered <strong>currently borrowed</strong> if there exists a<strong> </strong>borrowing record with a <strong>NULL</strong> <code>return_date</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the result table ordered by current borrowers in <strong>descending</strong> order, then by book title 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>library_books table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+---------+------------------------+------------------+----------+------------------+--------------+
|
||||
| book_id | title | author | genre | publication_year | total_copies |
|
||||
+---------+------------------------+------------------+----------+------------------+--------------+
|
||||
| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |
|
||||
| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 |
|
||||
| 3 | 1984 | George Orwell | Dystopian| 1949 | 1 |
|
||||
| 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 |
|
||||
| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 |
|
||||
| 6 | Brave New World | Aldous Huxley | Dystopian| 1932 | 4 |
|
||||
+---------+------------------------+------------------+----------+------------------+--------------+
|
||||
</pre>
|
||||
|
||||
<p>borrowing_records table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+-----------+---------+---------------+-------------+-------------+
|
||||
| record_id | book_id | borrower_name | borrow_date | return_date |
|
||||
+-----------+---------+---------------+-------------+-------------+
|
||||
| 1 | 1 | Alice Smith | 2024-01-15 | NULL |
|
||||
| 2 | 1 | Bob Johnson | 2024-01-20 | NULL |
|
||||
| 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 |
|
||||
| 4 | 3 | David Brown | 2024-02-01 | NULL |
|
||||
| 5 | 4 | Emma Wilson | 2024-01-05 | NULL |
|
||||
| 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 |
|
||||
| 7 | 1 | Grace Miller | 2024-02-05 | NULL |
|
||||
| 8 | 6 | Henry Taylor | 2024-01-12 | NULL |
|
||||
| 9 | 2 | Ivan Clark | 2024-02-12 | NULL |
|
||||
| 10 | 2 | Jane Adams | 2024-02-15 | NULL |
|
||||
+-----------+---------+---------------+-------------+-------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+---------+------------------+---------------+-----------+------------------+-------------------+
|
||||
| book_id | title | author | genre | publication_year | current_borrowers |
|
||||
+---------+------------------+---------------+-----------+------------------+-------------------+
|
||||
| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |
|
||||
| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |
|
||||
+---------+------------------+---------------+-----------+------------------+-------------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>The Great Gatsby (book_id = 1):</strong>
|
||||
|
||||
<ul>
|
||||
<li>Total copies: 3</li>
|
||||
<li>Currently borrowed by Alice Smith, Bob Johnson, and Grace Miller (3 borrowers)</li>
|
||||
<li>Available copies: 3 - 3 = 0</li>
|
||||
<li>Included because available_copies = 0</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>1984 (book_id = 3):</strong>
|
||||
<ul>
|
||||
<li>Total copies: 1</li>
|
||||
<li>Currently borrowed by David Brown (1 borrower)</li>
|
||||
<li>Available copies: 1 - 1 = 0</li>
|
||||
<li>Included because available_copies = 0</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Books not included:</strong>
|
||||
<ul>
|
||||
<li>To Kill a Mockingbird (book_id = 2): Total copies = 3, current borrowers = 2, available = 1</li>
|
||||
<li>Pride and Prejudice (book_id = 4): Total copies = 2, current borrowers = 1, available = 1</li>
|
||||
<li>The Catcher in the Rye (book_id = 5): Total copies = 1, current borrowers = 0, available = 1</li>
|
||||
<li>Brave New World (book_id = 6): Total copies = 4, current borrowers = 1, available = 3</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><strong>Result ordering:</strong>
|
||||
<ul>
|
||||
<li>The Great Gatsby appears first with 3 current borrowers</li>
|
||||
<li>1984 appears second with 1 current borrower</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Output table is ordered by current_borrowers in descending order, then by book_title in ascending order.</p>
|
||||
</div>
|
@@ -0,0 +1,185 @@
|
||||
<p>You are given an integer <code>n</code> and an <strong>undirected, weighted</strong> tree rooted at node 0 with <code>n</code> nodes numbered from 0 to <code>n - 1</code>. This is represented by a 2D array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates an edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
|
||||
|
||||
<p>The <strong>weighted median node</strong> is defined as the <strong>first</strong> node <code>x</code> on the path from <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> such that the sum of edge weights from <code>u<sub>i</sub></code> to <code>x</code> is <strong>greater than or equal to half</strong> of the total path weight.</p>
|
||||
|
||||
<p>You are given a 2D integer array <code>queries</code>. For each <code>queries[j] = [u<sub>j</sub>, v<sub>j</sub>]</code>, determine the weighted median node along the path from <code>u<sub>j</sub></code> to <code>v<sub>j</sub></code>.</p>
|
||||
|
||||
<p>Return an array <code>ans</code>, where <code>ans[j]</code> is the node index of the weighted median for <code>queries[j]</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 = 2, edges = [[0,1,7]], queries = [[1,0],[0,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2025/05/26/screenshot-2025-05-26-at-193447.png" style="width: 200px; height: 64px;" /></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Query</th>
|
||||
<th style="border: 1px solid black;">Path</th>
|
||||
<th style="border: 1px solid black;">Edge<br />
|
||||
Weights</th>
|
||||
<th style="border: 1px solid black;">Total<br />
|
||||
Path<br />
|
||||
Weight</th>
|
||||
<th style="border: 1px solid black;">Half</th>
|
||||
<th style="border: 1px solid black;">Explanation</th>
|
||||
<th style="border: 1px solid black;">Answer</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[1, 0]</code></td>
|
||||
<td style="border: 1px solid black;"><code>1 → 0</code></td>
|
||||
<td style="border: 1px solid black;"><code>[7]</code></td>
|
||||
<td style="border: 1px solid black;">7</td>
|
||||
<td style="border: 1px solid black;">3.5</td>
|
||||
<td style="border: 1px solid black;">Sum from <code>1 → 0 = 7 >= 3.5</code>, median is node 0.</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[0, 1]</code></td>
|
||||
<td style="border: 1px solid black;"><code>0 → 1</code></td>
|
||||
<td style="border: 1px solid black;"><code>[7]</code></td>
|
||||
<td style="border: 1px solid black;">7</td>
|
||||
<td style="border: 1px solid black;">3.5</td>
|
||||
<td style="border: 1px solid black;">Sum from <code>0 → 1 = 7 >= 3.5</code>, median is node 1.</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[2,0,4]], queries = [[0,1],[2,0],[1,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,0,2]</span></p>
|
||||
|
||||
<p><strong>E</strong><strong>xplanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2025/05/26/screenshot-2025-05-26-at-193610.png" style="width: 180px; height: 149px;" /></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Query</th>
|
||||
<th style="border: 1px solid black;">Path</th>
|
||||
<th style="border: 1px solid black;">Edge<br />
|
||||
Weights</th>
|
||||
<th style="border: 1px solid black;">Total<br />
|
||||
Path<br />
|
||||
Weight</th>
|
||||
<th style="border: 1px solid black;">Half</th>
|
||||
<th style="border: 1px solid black;">Explanation</th>
|
||||
<th style="border: 1px solid black;">Answer</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[0, 1]</code></td>
|
||||
<td style="border: 1px solid black;"><code>0 → 1</code></td>
|
||||
<td style="border: 1px solid black;"><code>[2]</code></td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">Sum from <code>0 → 1 = 2 >= 1</code>, median is node 1.</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[2, 0]</code></td>
|
||||
<td style="border: 1px solid black;"><code>2 → 0</code></td>
|
||||
<td style="border: 1px solid black;"><code>[4]</code></td>
|
||||
<td style="border: 1px solid black;">4</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">Sum from <code>2 → 0 = 4 >= 2</code>, median is node 0.</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[1, 2]</code></td>
|
||||
<td style="border: 1px solid black;"><code>1 → 0 → 2</code></td>
|
||||
<td style="border: 1px solid black;"><code>[2, 4]</code></td>
|
||||
<td style="border: 1px solid black;">6</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">Sum from <code>1 → 0 = 2 < 3</code>.<br />
|
||||
Sum from <code>1 → 2 = 2 + 4 = 6 >= 3</code>, median is node 2.</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[0,1,2],[0,2,5],[1,3,1],[2,4,3]], queries = [[3,4],[1,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2025/05/26/screenshot-2025-05-26-at-193857.png" style="width: 150px; height: 229px;" /></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Query</th>
|
||||
<th style="border: 1px solid black;">Path</th>
|
||||
<th style="border: 1px solid black;">Edge<br />
|
||||
Weights</th>
|
||||
<th style="border: 1px solid black;">Total<br />
|
||||
Path<br />
|
||||
Weight</th>
|
||||
<th style="border: 1px solid black;">Half</th>
|
||||
<th style="border: 1px solid black;">Explanation</th>
|
||||
<th style="border: 1px solid black;">Answer</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[3, 4]</code></td>
|
||||
<td style="border: 1px solid black;"><code>3 → 1 → 0 → 2 → 4</code></td>
|
||||
<td style="border: 1px solid black;"><code>[1, 2, 5, 3]</code></td>
|
||||
<td style="border: 1px solid black;">11</td>
|
||||
<td style="border: 1px solid black;">5.5</td>
|
||||
<td style="border: 1px solid black;">Sum from <code>3 → 1 = 1 < 5.5</code>.<br />
|
||||
Sum from <code>3 → 0 = 1 + 2 = 3 < 5.5</code>.<br />
|
||||
Sum from <code>3 → 2 = 1 + 2 + 5 = 8 >= 5.5</code>, median is node 2.</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;"><code>[1, 2]</code></td>
|
||||
<td style="border: 1px solid black;"><code>1 → 0 → 2</code></td>
|
||||
<td style="border: 1px solid black;"><code>[2, 5]</code></td>
|
||||
<td style="border: 1px solid black;">7</td>
|
||||
<td style="border: 1px solid black;">3.5</td>
|
||||
<td style="border: 1px solid black;">
|
||||
<p>Sum from <code>1 → 0 = 2 < 3.5</code>.<br />
|
||||
Sum from <code>1 → 2 = 2 + 5 = 7 >= 3.5</code>, median is node 2.</p>
|
||||
</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[j] == [u<sub>j</sub>, v<sub>j</sub>]</code></li>
|
||||
<li><code>0 <= u<sub>j</sub>, v<sub>j</sub> < n</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,84 @@
|
||||
<p data-end="324" data-start="147">You are given an <code>m x n</code> grid <code>classroom</code> where a student volunteer is tasked with cleaning up litter scattered around the room. Each cell in the grid is one of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'S'</code>: Starting position of the student</li>
|
||||
<li><code>'L'</code>: Litter that must be collected (once collected, the cell becomes empty)</li>
|
||||
<li><code>'R'</code>: Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)</li>
|
||||
<li><code>'X'</code>: Obstacle the student cannot pass through</li>
|
||||
<li><code>'.'</code>: Empty space</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer <code>energy</code>, representing the student's maximum energy capacity. The student starts with this energy from the starting position <code>'S'</code>.</p>
|
||||
|
||||
<p>Each move to an adjacent cell (up, down, left, or right) costs 1 unit of energy. If the energy reaches 0, the student can only continue if they are on a reset area <code>'R'</code>, which resets the energy to its <strong>maximum</strong> capacity <code>energy</code>.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of moves required to collect all litter items, or <code>-1</code> if it's impossible.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">classroom = ["S.", "XL"], energy = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The student starts at cell <code data-end="262" data-start="254">(0, 0)</code> with 2 units of energy.</li>
|
||||
<li>Since cell <code>(1, 0)</code> contains an obstacle 'X', the student cannot move directly downward.</li>
|
||||
<li>A valid sequence of moves to collect all litter is as follows:
|
||||
<ul>
|
||||
<li>Move 1: From <code>(0, 0)</code> → <code>(0, 1)</code> with 1 unit of energy and 1 unit remaining.</li>
|
||||
<li>Move 2: From <code>(0, 1)</code> → <code>(1, 1)</code> to collect the litter <code>'L'</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>The student collects all the litter using 2 moves. Thus, the output 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">classroom = ["LS", "RL"], energy = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The student starts at cell <code data-end="262" data-start="254">(0, 1)</code> with 4 units of energy.</li>
|
||||
<li>A valid sequence of moves to collect all litter is as follows:
|
||||
<ul>
|
||||
<li>Move 1: From <code>(0, 1)</code> → <code>(0, 0)</code> to collect the first litter <code>'L'</code> with 1 unit of energy used and 3 units remaining.</li>
|
||||
<li>Move 2: From <code>(0, 0)</code> → <code>(1, 0)</code> to <code>'R'</code> to reset and restore energy back to 4.</li>
|
||||
<li>Move 3: From <code>(1, 0)</code> → <code>(1, 1)</code> to collect the second litter <code data-end="1068" data-start="1063">'L'</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>The student collects all the litter using 3 moves. Thus, the output 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">classroom = ["L.S", "RXL"], energy = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>No valid path collects all <code>'L'</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m == classroom.length <= 20</code></li>
|
||||
<li><code>1 <= n == classroom[i].length <= 20</code></li>
|
||||
<li><code>classroom[i][j]</code> is one of <code>'S'</code>, <code>'L'</code>, <code>'R'</code>, <code>'X'</code>, or <code>'.'</code></li>
|
||||
<li><code>1 <= energy <= 50</code></li>
|
||||
<li>There is exactly <strong>one</strong> <code>'S'</code> in the grid.</li>
|
||||
<li>There are <strong>at most</strong> 10 <code>'L'</code> cells in the grid.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given an integer array <code>nums</code> containing <strong>distinct</strong> positive integers and an integer <code>target</code>.</p>
|
||||
|
||||
<p>Determine if you can partition <code>nums</code> into two <strong>non-empty</strong> <strong>disjoint</strong> <strong>subsets</strong>, with each element belonging to <strong>exactly one</strong> subset, such that the product of the elements in each subset is equal to <code>target</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> if such a partition exists and <code>false</code> otherwise.</p>
|
||||
A <strong>subset</strong> of an array is a selection of elements of the array.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,1,6,8,4], target = 24</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> The subsets <code>[3, 8]</code> and <code>[1, 6, 4]</code> each have a product of 24. Hence, the output is true.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,3,7], target = 15</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> There is no way to partition <code>nums</code> into two non-empty disjoint subsets such that both subsets have a product of 15. Hence, the output is false.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 12</code></li>
|
||||
<li><code>1 <= target <= 10<sup>15</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li>All elements of <code>nums</code> are <strong>distinct</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Your task is to partition <code>nums</code> into one or more <strong>non-empty</strong> contiguous segments such that in each segment, the difference between its <strong>maximum</strong> and <strong>minimum</strong> elements is <strong>at most</strong> <code>k</code>.</p>
|
||||
|
||||
<p>Return the total number of ways to partition <code>nums</code> under this condition.</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 = [9,4,1,3,7], k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 6 valid partitions where the difference between the maximum and minimum elements in each segment is at most <code>k = 4</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>[[9], [4], [1], [3], [7]]</code></li>
|
||||
<li><code>[[9], [4], [1], [3, 7]]</code></li>
|
||||
<li><code>[[9], [4], [1, 3], [7]]</code></li>
|
||||
<li><code>[[9], [4, 1], [3], [7]]</code></li>
|
||||
<li><code>[[9], [4, 1], [3, 7]]</code></li>
|
||||
<li><code>[[9], [4, 1, 3], [7]]</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 = [3,3,4], k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 2 valid partitions that satisfy the given conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>[[3], [3], [4]]</code></li>
|
||||
<li><code>[[3, 3], [4]]</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,88 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>special triplet</strong> is defined as a triplet of indices <code>(i, j, k)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < k < n</code>, where <code>n = nums.length</code></li>
|
||||
<li><code>nums[i] == nums[j] * 2</code></li>
|
||||
<li><code>nums[k] == nums[j] * 2</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return the total number of <strong>special triplets</strong> in the array.</p>
|
||||
|
||||
<p>Since the answer may be 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 = [6,3,6]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only special triplet is <code>(i, j, k) = (0, 1, 2)</code>, where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[0] = 6</code>, <code>nums[1] = 3</code>, <code>nums[2] = 6</code></li>
|
||||
<li><code>nums[0] = nums[1] * 2 = 3 * 2 = 6</code></li>
|
||||
<li><code>nums[2] = nums[1] * 2 = 3 * 2 = 6</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 = [0,1,0,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only special triplet is <code>(i, j, k) = (0, 2, 3)</code>, where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[0] = 0</code>, <code>nums[2] = 0</code>, <code>nums[3] = 0</code></li>
|
||||
<li><code>nums[0] = nums[2] * 2 = 0 * 2 = 0</code></li>
|
||||
<li><code>nums[3] = nums[2] * 2 = 0 * 2 = 0</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [8,4,2,8,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are exactly two special triplets:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>(i, j, k) = (0, 1, 3)</code>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[0] = 8</code>, <code>nums[1] = 4</code>, <code>nums[3] = 8</code></li>
|
||||
<li><code>nums[0] = nums[1] * 2 = 4 * 2 = 8</code></li>
|
||||
<li><code>nums[3] = nums[1] * 2 = 4 * 2 = 8</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>(i, j, k) = (1, 2, 4)</code>
|
||||
<ul>
|
||||
<li><code>nums[1] = 4</code>, <code>nums[2] = 2</code>, <code>nums[4] = 4</code></li>
|
||||
<li><code>nums[1] = nums[2] * 2 = 2 * 2 = 4</code></li>
|
||||
<li><code>nums[4] = nums[2] * 2 = 2 * 2 = 4</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,66 @@
|
||||
<p>You are given an array <code>complexity</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>There are <code>n</code> <strong>locked</strong> computers in a room with labels from 0 to <code>n - 1</code>, each with its own <strong>unique</strong> password. The password of the computer <code>i</code> has a complexity <code>complexity[i]</code>.</p>
|
||||
|
||||
<p>The password for the computer labeled 0 is <strong>already</strong> decrypted and serves as the root. All other computers must be unlocked using it or another previously unlocked computer, following this information:</p>
|
||||
|
||||
<ul>
|
||||
<li>You can decrypt the password for the computer <code>i</code> using the password for computer <code>j</code>, where <code>j</code> is <strong>any</strong> integer less than <code>i</code> with a lower complexity. (i.e. <code>j < i</code> and <code>complexity[j] < complexity[i]</code>)</li>
|
||||
<li>To decrypt the password for computer <code>i</code>, you must have already unlocked a computer <code>j</code> such that <code>j < i</code> and <code>complexity[j] < complexity[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Find the number of <span data-keyword="permutation-array">permutations</span> of <code>[0, 1, 2, ..., (n - 1)]</code> that represent a valid order in which the computers can be unlocked, starting from computer 0 as the only initially unlocked one.</p>
|
||||
|
||||
<p>Since the answer may be large, return it <strong>modulo</strong> 10<sup>9</sup> + 7.</p>
|
||||
|
||||
<p><strong>Note</strong> that the password for the computer <strong>with label</strong> 0 is decrypted, and <em>not</em> the computer with the first position in the permutation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">complexity = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The valid permutations are:</p>
|
||||
|
||||
<ul>
|
||||
<li>[0, 1, 2]
|
||||
<ul>
|
||||
<li>Unlock computer 0 first with root password.</li>
|
||||
<li>Unlock computer 1 with password of computer 0 since <code>complexity[0] < complexity[1]</code>.</li>
|
||||
<li>Unlock computer 2 with password of computer 1 since <code>complexity[1] < complexity[2]</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>[0, 2, 1]
|
||||
<ul>
|
||||
<li>Unlock computer 0 first with root password.</li>
|
||||
<li>Unlock computer 2 with password of computer 0 since <code>complexity[0] < complexity[2]</code>.</li>
|
||||
<li>Unlock computer 1 with password of computer 0 since <code>complexity[0] < complexity[1]</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">complexity = [3,3,3,4,4,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are no possible permutations which can unlock all computers.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= complexity.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= complexity[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>Table: <code>Courses</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| student | varchar |
|
||||
| class | varchar |
|
||||
+-------------+---------+
|
||||
(student, class) is the primary key (combination of columns with unique values) for this table.
|
||||
Each row of this table indicates the name of a student and the class in which they are enrolled.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find all the classes that have <strong>at least five students</strong>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Courses table:
|
||||
+---------+----------+
|
||||
| student | class |
|
||||
+---------+----------+
|
||||
| A | Math |
|
||||
| B | English |
|
||||
| C | Math |
|
||||
| D | Biology |
|
||||
| E | Math |
|
||||
| F | Computer |
|
||||
| G | Math |
|
||||
| H | Math |
|
||||
| I | Math |
|
||||
+---------+----------+
|
||||
<strong>Output:</strong>
|
||||
+---------+
|
||||
| class |
|
||||
+---------+
|
||||
| Math |
|
||||
+---------+
|
||||
<strong>Explanation:</strong>
|
||||
- Math has 6 students, so we include it.
|
||||
- English has 1 student, so we do not include it.
|
||||
- Biology has 1 student, so we do not include it.
|
||||
- Computer has 1 student, so we do not include it.
|
||||
</pre>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given two integer arrays <code>x</code> and <code>y</code>, each of length <code>n</code>. You must choose three <strong>distinct</strong> indices <code>i</code>, <code>j</code>, and <code>k</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>x[i] != x[j]</code></li>
|
||||
<li><code>x[j] != x[k]</code></li>
|
||||
<li><code>x[k] != x[i]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Your goal is to <strong>maximize</strong> the value of <code>y[i] + y[j] + y[k]</code> under these conditions. Return the <strong>maximum</strong> possible sum that can be obtained by choosing such a triplet of indices.</p>
|
||||
|
||||
<p>If no such triplet exists, return -1.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = [1,2,1,3,2], y = [5,3,4,6,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">14</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <code>i = 0</code> (<code>x[i] = 1</code>, <code>y[i] = 5</code>), <code>j = 1</code> (<code>x[j] = 2</code>, <code>y[j] = 3</code>), <code>k = 3</code> (<code>x[k] = 3</code>, <code>y[k] = 6</code>).</li>
|
||||
<li>All three values chosen from <code>x</code> are distinct. <code>5 + 3 + 6 = 14</code> is the maximum we can obtain. Hence, the output is 14.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">x = [1,2,1,2], y = [4,5,6,7]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>There are only two distinct values in <code>x</code>. Hence, the output is -1.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == x.length == y.length</code></li>
|
||||
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= x[i], y[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user