mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
53
leetcode/problem/button-with-longest-push-time.html
Normal file
53
leetcode/problem/button-with-longest-push-time.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<p>You are given a 2D array <code>events</code> which represents a sequence of events where a child pushes a series of buttons on a keyboard.</p>
|
||||
|
||||
<p>Each <code>events[i] = [index<sub>i</sub>, time<sub>i</sub>]</code> indicates that the button at index <code>index<sub>i</sub></code> was pressed at time <code>time<sub>i</sub></code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>The array is <strong>sorted</strong> in increasing order of <code>time</code>.</li>
|
||||
<li>The time taken to press a button is the difference in time between consecutive button presses. The time for the first button is simply the time at which it was pressed.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <code>index</code> of the button that took the <strong>longest</strong> time to push. If multiple buttons have the same longest time, return the button with the <strong>smallest</strong> <code>index</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">events = [[1,2],[2,5],[3,9],[1,15]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Button with index 1 is pressed at time 2.</li>
|
||||
<li>Button with index 2 is pressed at time 5, so it took <code>5 - 2 = 3</code> units of time.</li>
|
||||
<li>Button with index 3 is pressed at time 9, so it took <code>9 - 5 = 4</code> units of time.</li>
|
||||
<li>Button with index 1 is pressed again at time 15, so it took <code>15 - 9 = 6</code> units of time.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">events = [[10,5],[1,7]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Button with index 10 is pressed at time 5.</li>
|
||||
<li>Button with index 1 is pressed at time 7, so it took <code>7 - 5 = 2</code> units of time.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= events.length <= 1000</code></li>
|
||||
<li><code>events[i] == [index<sub>i</sub>, time<sub>i</sub>]</code></li>
|
||||
<li><code>1 <= index<sub>i</sub>, time<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li>The input is generated such that <code>events</code> is sorted in increasing order of <code>time<sub>i</sub></code>.</li>
|
||||
</ul>
|
48
leetcode/problem/count-beautiful-splits-in-an-array.html
Normal file
48
leetcode/problem/count-beautiful-splits-in-an-array.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>You are given an array <code>nums</code>.</p>
|
||||
|
||||
<p>A split of an array <code>nums</code> is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ol>
|
||||
<li>The array <code>nums</code> is split into three <span data-keyword="subarray-nonempty">subarrays</span>: <code>nums1</code>, <code>nums2</code>, and <code>nums3</code>, such that <code>nums</code> can be formed by concatenating <code>nums1</code>, <code>nums2</code>, and <code>nums3</code> in that order.</li>
|
||||
<li>The subarray <code>nums1</code> is a <span data-keyword="array-prefix">prefix</span> of <code>nums2</code> <strong>OR</strong> <code>nums2</code> is a <span data-keyword="array-prefix">prefix</span> of <code>nums3</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return the <strong>number of ways</strong> you can make this split.</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,2,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The beautiful splits are:</p>
|
||||
|
||||
<ol>
|
||||
<li>A split with <code>nums1 = [1]</code>, <code>nums2 = [1,2]</code>, <code>nums3 = [1]</code>.</li>
|
||||
<li>A split with <code>nums1 = [1]</code>, <code>nums2 = [1]</code>, <code>nums3 = [2,1]</code>.</li>
|
||||
</ol>
|
||||
</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]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There are 0 beautiful splits.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5000</code></li>
|
||||
<li><code><font face="monospace">0 <= nums[i] <= 50</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an array of integers <code>nums</code> of size <code>n</code> and a <strong>positive</strong> integer <code>threshold</code>.</p>
|
||||
|
||||
<p>There is a graph consisting of <code>n</code> nodes with the <code>i<sup>th</sup></code> node having a value of <code>nums[i]</code>. Two nodes <code>i</code> and <code>j</code> in the graph are connected via an <strong>undirected</strong> edge if <code>lcm(nums[i], nums[j]) <= threshold</code>.</p>
|
||||
|
||||
<p>Return the number of <strong>connected components</strong> in this graph.</p>
|
||||
|
||||
<p>A <strong>connected component</strong> is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.</p>
|
||||
|
||||
<p>The term <code>lcm(a, b)</code> denotes the <strong>least common multiple</strong> of <code>a</code> and <code>b</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,4,8,3,9], threshold = 5</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> </p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/10/31/example0.png" style="width: 250px; height: 251px;" /></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>The four connected components are <code>(2, 4)</code>, <code>(3)</code>, <code>(8)</code>, <code>(9)</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,8,3,9,12], threshold = 10</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> </p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/10/31/example1.png" style="width: 250px; height: 252px;" /></p>
|
||||
|
||||
<p>The two connected components are <code>(2, 3, 4, 8, 9)</code>, and <code>(12)</code>.</p>
|
||||
</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>9</sup></code></li>
|
||||
<li>All elements of <code>nums</code> are unique.</li>
|
||||
<li><code>1 <= threshold <= 2 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,66 @@
|
||||
<p>You are given two integers <code>n</code> and <code>m</code> that consist of the <strong>same</strong> number of digits.</p>
|
||||
|
||||
<p>You can perform the following operations <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <strong>any</strong> digit from <code>n</code> that is not 9 and <strong>increase</strong> it by 1.</li>
|
||||
<li>Choose <strong>any</strong> digit from <code>n</code> that is not 0 and <strong>decrease</strong> it by 1.</li>
|
||||
</ul>
|
||||
|
||||
<p>The integer <code>n</code> must not be a <span data-keyword="prime-number">prime</span> number at any point, including its original value and after each operation.</p>
|
||||
|
||||
<p>The cost of a transformation is the sum of <strong>all</strong> values that <code>n</code> takes throughout the operations performed.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> cost to transform <code>n</code> into <code>m</code>. If it is impossible, 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">n = 10, m = 12</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">85</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase the first digit, now <code>n = <u><strong>2</strong></u>0</code>.</li>
|
||||
<li>Increase the second digit, now <code>n = 2<strong><u>1</u></strong></code>.</li>
|
||||
<li>Increase the second digit, now <code>n = 2<strong><u>2</u></strong></code>.</li>
|
||||
<li>Decrease the first digit, now <code>n = <strong><u>1</u></strong>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">n = 4, m = 8</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>It is impossible to make <code>n</code> equal to <code>m</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 6, m = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> </p>
|
||||
|
||||
<p>Since 2 is already a prime, we can't make <code>n</code> equal to <code>m</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, m < 10<sup>4</sup></code></li>
|
||||
<li><code>n</code> and <code>m</code> consist of the same number of digits.</li>
|
||||
</ul>
|
89
leetcode/problem/first-letter-capitalization-ii.html
Normal file
89
leetcode/problem/first-letter-capitalization-ii.html
Normal file
@@ -0,0 +1,89 @@
|
||||
<p>Table: <code>user_content</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| content_id | int |
|
||||
| content_text| varchar |
|
||||
+-------------+---------+
|
||||
content_id is the unique key for this table.
|
||||
Each row contains a unique ID and the corresponding text content.
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to transform the text in the <code>content_text</code> column by applying the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>Convert the <strong>first letter</strong> of each word to <strong>uppercase</strong> and the <strong>remaining</strong> letters to <strong>lowercase</strong></li>
|
||||
<li>Special handling for words containing special characters:
|
||||
<ul>
|
||||
<li>For words connected with a hyphen <code>-</code>, <strong>both parts</strong> should be <strong>capitalized</strong> (<strong>e.g.</strong>, top-rated → Top-Rated)</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>All other <strong>formatting</strong> and <strong>spacing</strong> should remain <strong>unchanged</strong></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the result table that includes both the original <code>content_text</code> and the modified text following the above rules</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>user_content table:</p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------+---------------------------------+
|
||||
| content_id | content_text |
|
||||
+------------+---------------------------------+
|
||||
| 1 | hello world of SQL |
|
||||
| 2 | the QUICK-brown fox |
|
||||
| 3 | modern-day DATA science |
|
||||
| 4 | web-based FRONT-end development |
|
||||
+------------+---------------------------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Output:</strong></p>
|
||||
|
||||
<pre class="example-io">
|
||||
+------------+---------------------------------+---------------------------------+
|
||||
| content_id | original_text | converted_text |
|
||||
+------------+---------------------------------+---------------------------------+
|
||||
| 1 | hello world of SQL | Hello World Of Sql |
|
||||
| 2 | the QUICK-brown fox | The Quick-Brown Fox |
|
||||
| 3 | modern-day DATA science | Modern-Day Data Science |
|
||||
| 4 | web-based FRONT-end development | Web-Based Front-End Development |
|
||||
+------------+---------------------------------+---------------------------------+
|
||||
</pre>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For content_id = 1:
|
||||
<ul>
|
||||
<li>Each word's first letter is capitalized: "Hello World Of Sql"</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For content_id = 2:
|
||||
<ul>
|
||||
<li>Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown"</li>
|
||||
<li>Other words follow normal capitalization rules</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For content_id = 3:
|
||||
<ul>
|
||||
<li>Hyphenated word "modern-day" becomes "Modern-Day"</li>
|
||||
<li>"DATA" is converted to "Data"</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>For content_id = 4:
|
||||
<ul>
|
||||
<li>Contains two hyphenated words: "web-based" → "Web-Based"</li>
|
||||
<li>And "FRONT-end" → "Front-End"</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
@@ -0,0 +1,53 @@
|
||||
<p>You are given an integer array <code>nums</code>. This array contains <code>n</code> elements, where <strong>exactly</strong> <code>n - 2</code> elements are <strong>special</strong><strong> numbers</strong>. One of the remaining <strong>two</strong> elements is the <em>sum</em> of these <strong>special numbers</strong>, and the other is an <strong>outlier</strong>.</p>
|
||||
|
||||
<p>An <strong>outlier</strong> is defined as a number that is <em>neither</em> one of the original special numbers <em>nor</em> the element representing the sum of those numbers.</p>
|
||||
|
||||
<p><strong>Note</strong> that special numbers, the sum element, and the outlier must have <strong>distinct</strong> indices, but <em>may </em>share the <strong>same</strong> value.</p>
|
||||
|
||||
<p>Return the <strong>largest</strong><strong> </strong>potential<strong> outlier</strong> 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,3,5,10]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The special numbers could be 2 and 3, thus making their sum 5 and the outlier 10.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [-2,-1,-3,-6,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The special numbers could be -2, -1, and -3, thus making their sum -6 and the outlier 4.</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,1,1,5,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The special numbers could be 1, 1, 1, 1, and 1, thus making their sum 5 and the other 5 as the outlier.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li>The input is generated such that at least <strong>one</strong> potential outlier exists in <code>nums</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,87 @@
|
||||
<p>You are given a string <code>initialCurrency</code>, and you start with <code>1.0</code> of <code>initialCurrency</code>.</p>
|
||||
|
||||
<p>You are also given four arrays with currency pairs (strings) and rates (real numbers):</p>
|
||||
|
||||
<ul>
|
||||
<li><code>pairs1[i] = [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code> denotes that you can convert from <code>startCurrency<sub>i</sub></code> to <code>targetCurrency<sub>i</sub></code> at a rate of <code>rates1[i]</code> on <strong>day 1</strong>.</li>
|
||||
<li><code>pairs2[i] = [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code> denotes that you can convert from <code>startCurrency<sub>i</sub></code> to <code>targetCurrency<sub>i</sub></code> at a rate of <code>rates2[i]</code> on <strong>day 2</strong>.</li>
|
||||
<li>Also, each <code>targetCurrency</code> can be converted back to its corresponding <code>startCurrency</code> at a rate of <code>1 / rate</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can perform <strong>any</strong> number of conversions, <strong>including zero</strong>, using <code>rates1</code> on day 1, <strong>followed</strong> by any number of additional conversions, <strong>including zero</strong>, using <code>rates2</code> on day 2.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> amount of <code>initialCurrency</code> you can have after performing any number of conversions on both days <strong>in order</strong>.</p>
|
||||
|
||||
<p><strong>Note: </strong>Conversion rates are valid, and there will be no contradictions in the rates for either day. The rates for the days are independent of each other.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">initialCurrency = "EUR", pairs1 = [["EUR","USD"],["USD","JPY"]], rates1 = [2.0,3.0], pairs2 = [["JPY","USD"],["USD","CHF"],["CHF","EUR"]], rates2 = [4.0,5.0,6.0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">720.00000</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>To get the maximum amount of <strong>EUR</strong>, starting with 1.0 <strong>EUR</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>On Day 1:
|
||||
<ul>
|
||||
<li>Convert <strong>EUR </strong>to <strong>USD</strong> to get 2.0 <strong>USD</strong>.</li>
|
||||
<li>Convert <strong>USD</strong> to <strong>JPY</strong> to get 6.0 <strong>JPY</strong>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>On Day 2:
|
||||
<ul>
|
||||
<li>Convert <strong>JPY</strong> to <strong>USD</strong> to get 24.0 <strong>USD</strong>.</li>
|
||||
<li>Convert <strong>USD</strong> to <strong>CHF</strong> to get 120.0 <strong>CHF</strong>.</li>
|
||||
<li>Finally, convert <strong>CHF</strong> to <strong>EUR</strong> to get 720.0 <strong>EUR</strong>.</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">initialCurrency = "NGN", pairs1 = </span>[["NGN","EUR"]]<span class="example-io">, rates1 = </span>[9.0]<span class="example-io">, pairs2 = </span>[["NGN","EUR"]]<span class="example-io">, rates2 = </span>[6.0]</p>
|
||||
|
||||
<p><strong>Output:</strong> 1.50000</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Converting <strong>NGN</strong> to <strong>EUR</strong> on day 1 and <strong>EUR</strong> to <strong>NGN</strong> using the inverse rate on day 2 gives the maximum amount.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">initialCurrency = "USD", pairs1 = [["USD","EUR"]], rates1 = [1.0], pairs2 = [["EUR","JPY"]], rates2 = [10.0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1.00000</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>In this example, there is no need to make any conversions on either day.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= initialCurrency.length <= 3</code></li>
|
||||
<li><code>initialCurrency</code> consists only of uppercase English letters.</li>
|
||||
<li><code>1 <= n == pairs1.length <= 10</code></li>
|
||||
<li><code>1 <= m == pairs2.length <= 10</code></li>
|
||||
<li><code>pairs1[i] == [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code><!-- notionvc: c31b5bb8-4df6-4987-9bcd-6dff8a5f7cd4 --></li>
|
||||
<li><code>pairs2[i] == [startCurrency<sub>i</sub>, targetCurrency<sub>i</sub>]</code><!--{C}%3C!%2D%2D%20notionvc%3A%20c31b5bb8-4df6-4987-9bcd-6dff8a5f7cd4%20%2D%2D%3E--></li>
|
||||
<li><code>1 <= startCurrency<sub>i</sub>.length, targetCurrency<sub>i</sub>.length <= 3</code></li>
|
||||
<li><code>startCurrency<sub>i</sub></code> and <code>targetCurrency<sub>i</sub></code> consist only of uppercase English letters.</li>
|
||||
<li><code>rates1.length == n</code></li>
|
||||
<li><code>rates2.length == m</code></li>
|
||||
<li><code>1.0 <= rates1[i], rates2[i] <= 10.0</code></li>
|
||||
<li>The input is generated such that there are no contradictions or cycles in the conversion graphs for either day.</li>
|
||||
<li>The input is generated such that the output is <strong>at most</strong> <code>5 * 10<sup>10</sup></code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, with <strong>distinct</strong> labels in ranges <code>[0, n - 1]</code> and <code>[0, m - 1]</code>, respectively.</p>
|
||||
|
||||
<p>You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code> and <code>m - 1</code>, respectively, where <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree. You are also given an integer <code>k</code>.</p>
|
||||
|
||||
<p>Node <code>u</code> is <strong>target</strong> to node <code>v</code> if the number of edges on the path from <code>u</code> to <code>v</code> is less than or equal to <code>k</code>. <strong>Note</strong> that a node is <em>always</em> <strong>target</strong> to itself.</p>
|
||||
|
||||
<p>Return an array of <code>n</code> integers <code>answer</code>, where <code>answer[i]</code> is the <strong>maximum</strong> possible number of nodes <strong>target</strong> to node <code>i</code> of the first tree if you have to connect one node from the first tree to another node in the second tree.</p>
|
||||
|
||||
<p><strong>Note</strong> that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[9,7,9,8,8]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>, connect node 0 from the first tree to node 0 from the second tree.</li>
|
||||
<li>For <code>i = 1</code>, connect node 1 from the first tree to node 0 from the second tree.</li>
|
||||
<li>For <code>i = 2</code>, connect node 2 from the first tree to node 4 from the second tree.</li>
|
||||
<li>For <code>i = 3</code>, connect node 3 from the first tree to node 4 from the second tree.</li>
|
||||
<li>For <code>i = 4</code>, connect node 4 from the first tree to node 4 from the second tree.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/24/3982-1.png" style="width: 600px; height: 169px;" /></div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[6,3,3,3,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>For every <code>i</code>, connect node <code>i</code> of the first tree with any node of the second tree.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/24/3928-2.png" style="height: 281px; width: 500px;" /></div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n, m <= 1000</code></li>
|
||||
<li><code>edges1.length == n - 1</code></li>
|
||||
<li><code>edges2.length == m - 1</code></li>
|
||||
<li><code>edges1[i].length == edges2[i].length == 2</code></li>
|
||||
<li><code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code></li>
|
||||
<li>The input is generated such that <code>edges1</code> and <code>edges2</code> represent valid trees.</li>
|
||||
<li><code>0 <= k <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, labeled from <code>[0, n - 1]</code> and <code>[0, m - 1]</code>, respectively.</p>
|
||||
|
||||
<p>You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code> and <code>m - 1</code>, respectively, where <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree.</p>
|
||||
|
||||
<p>Node <code>u</code> is <strong>target</strong> to node <code>v</code> if the number of edges on the path from <code>u</code> to <code>v</code> is even. <strong>Note</strong> that a node is <em>always</em> <strong>target</strong> to itself.</p>
|
||||
|
||||
<p>Return an array of <code>n</code> integers <code>answer</code>, where <code>answer[i]</code> is the <strong>maximum</strong> possible number of nodes that are <strong>target</strong> to node <code>i</code> of the first tree if you had to connect one node from the first tree to another node in the second tree.</p>
|
||||
|
||||
<p><strong>Note</strong> that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[8,7,7,8,8]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>, connect node 0 from the first tree to node 0 from the second tree.</li>
|
||||
<li>For <code>i = 1</code>, connect node 1 from the first tree to node 4 from the second tree.</li>
|
||||
<li>For <code>i = 2</code>, connect node 2 from the first tree to node 7 from the second tree.</li>
|
||||
<li>For <code>i = 3</code>, connect node 3 from the first tree to node 0 from the second tree.</li>
|
||||
<li>For <code>i = 4</code>, connect node 4 from the first tree to node 4 from the second tree.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/24/3982-1.png" style="width: 600px; height: 169px;" /></div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,6,6,6,6]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>For every <code>i</code>, connect node <code>i</code> of the first tree with any node of the second tree.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/24/3928-2.png" style="height: 281px; width: 500px;" /></div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges1.length == n - 1</code></li>
|
||||
<li><code>edges2.length == m - 1</code></li>
|
||||
<li><code>edges1[i].length == edges2[i].length == 2</code></li>
|
||||
<li><code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code></li>
|
||||
<li>The input is generated such that <code>edges1</code> and <code>edges2</code> represent valid trees.</li>
|
||||
</ul>
|
@@ -0,0 +1,64 @@
|
||||
<p>You are given an array <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of a point on an infinite plane.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum </strong>area of a rectangle that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Can be formed using <strong>four</strong> of these points as its corners.</li>
|
||||
<li>Does <strong>not</strong> contain any other point inside or on its border.</li>
|
||||
<li>Has its edges <strong>parallel</strong> to the axes.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>maximum area</strong> that you can obtain or -1 if no such rectangle is possible.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[1,1],[1,3],[3,1],[3,3]]</span></p>
|
||||
|
||||
<p><strong>Output: </strong>4</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="Example 1 diagram" src="https://assets.leetcode.com/uploads/2024/11/02/example1.png" style="width: 229px; height: 228px;" /></strong></p>
|
||||
|
||||
<p>We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border<!-- notionvc: f270d0a3-a596-4ed6-9997-2c7416b2b4ee -->. Hence, the maximum possible area would be 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[1,1],[1,3],[3,1],[3,3],[2,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong><b> </b>-1</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="Example 2 diagram" src="https://assets.leetcode.com/uploads/2024/11/02/example2.png" style="width: 229px; height: 228px;" /></strong></p>
|
||||
|
||||
<p>There is only one rectangle possible is with points <code>[1,1], [1,3], [3,1]</code> and <code>[3,3]</code> but <code>[2,2]</code> will always lie inside it. Hence, returning -1.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]</span></p>
|
||||
|
||||
<p><strong>Output: </strong>2</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="Example 3 diagram" src="https://assets.leetcode.com/uploads/2024/11/02/example3.png" style="width: 229px; height: 228px;" /></strong></p>
|
||||
|
||||
<p>The maximum area rectangle is formed by the points <code>[1,3], [1,2], [3,2], [3,3]</code>, which has an area of 2. Additionally, the points <code>[1,1], [1,2], [3,1], [3,2]</code> also form a valid rectangle with the same area.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= points.length <= 10</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code></li>
|
||||
<li>All the given points are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>There are n points on an infinite plane. You are given two integer arrays <code>xCoord</code> and <code>yCoord</code> where <code>(xCoord[i], yCoord[i])</code> represents the coordinates of the <code>i<sup>th</sup></code> point.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum </strong>area of a rectangle that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Can be formed using <strong>four</strong> of these points as its corners.</li>
|
||||
<li>Does <strong>not</strong> contain any other point inside or on its border.</li>
|
||||
<li>Has its edges <strong>parallel</strong> to the axes.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>maximum area</strong> that you can obtain or -1 if no such rectangle is possible.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">xCoord = [1,1,3,3], yCoord = [1,3,1,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="Example 1 diagram" src="https://assets.leetcode.com/uploads/2024/11/02/example1.png" style="width: 229px; height: 228px;" /></strong></p>
|
||||
|
||||
<p>We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="Example 2 diagram" src="https://assets.leetcode.com/uploads/2024/11/02/example2.png" style="width: 229px; height: 228px;" /></strong></p>
|
||||
|
||||
<p>There is only one rectangle possible is with points <code>[1,1], [1,3], [3,1]</code> and <code>[3,3]</code> but <code>[2,2]</code> will always lie inside it. Hence, returning -1.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="Example 3 diagram" src="https://assets.leetcode.com/uploads/2024/11/02/example3.png" style="width: 229px; height: 228px;" /></strong></p>
|
||||
|
||||
<p>The maximum area rectangle is formed by the points <code>[1,3], [1,2], [3,2], [3,3]</code>, which has an area of 2. Additionally, the points <code>[1,1], [1,2], [3,1], [3,2]</code> also form a valid rectangle with the same area.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= xCoord.length == yCoord.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= xCoord[i], yCoord[i] <= 8 * 10<sup>7</sup></code></li>
|
||||
<li>All the given points are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given an array of integers <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> sum of a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code>, such that the size of the subarray is <strong>divisible</strong> by <code>k</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,2], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The subarray <code>[1, 2]</code> with sum 3 has length equal to 2 which is divisible by 1.</p>
|
||||
</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 = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The maximum sum subarray is <code>[-1, -2, -3, -4]</code> which has length equal to 4 which is divisible by 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [-5,1,2,-3,4], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The maximum sum subarray is <code>[1, 2, -3, 4]</code> which has length equal to 4 which is divisible by 2.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An integer <code>h</code> is called <strong>valid</strong> if all values in the array that are <strong>strictly greater</strong> than <code>h</code> are <em>identical</em>.</p>
|
||||
|
||||
<p>For example, if <code>nums = [10, 8, 10, 8]</code>, a <strong>valid</strong> integer is <code>h = 9</code> because all <code>nums[i] > 9</code> are equal to 10, but 5 is not a <strong>valid</strong> integer.</p>
|
||||
|
||||
<p>You are allowed to perform the following operation on <code>nums</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select an integer <code>h</code> that is <em>valid</em> for the <strong>current</strong> values in <code>nums</code>.</li>
|
||||
<li>For each index <code>i</code> where <code>nums[i] > h</code>, set <code>nums[i]</code> to <code>h</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations required to make every element in <code>nums</code> <strong>equal</strong> to <code>k</code>. If it is impossible to make all elements equal to <code>k</code>, 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">nums = [5,2,5,4,5], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The operations can be performed in order using valid integers 4 and then 2.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>It is impossible to make all the values equal to 2.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [9,7,5,3], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The operations can be performed using valid integers in the order 7, 5, 3, and 1.</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>
|
||||
<li><code>1 <= k <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,65 @@
|
||||
<p>You are given a string <code>s</code>.</p>
|
||||
|
||||
<p>A string <code>t</code> is called <strong>good</strong> if all characters of <code>t</code> occur the same number of times.</p>
|
||||
|
||||
<p>You can perform the following operations <strong>any number of times</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Delete a character from <code>s</code>.</li>
|
||||
<li>Insert a character in <code>s</code>.</li>
|
||||
<li>Change a character in <code>s</code> to its next letter in the alphabet.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that you cannot change <code>'z'</code> to <code>'a'</code> using the third operation.</p>
|
||||
|
||||
<p>Return<em> </em>the <strong>minimum</strong> number of operations required to make <code>s</code> <strong>good</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "acab"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can make <code>s</code> good by deleting one occurrence of character <code>'a'</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "wddw"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We do not need to perform any operations since <code>s</code> is initially good.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "aaabc"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can make <code>s</code> good by applying these operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Change one occurrence of <code>'a'</code> to <code>'b'</code></li>
|
||||
<li>Insert one occurrence of <code>'c'</code> into <code>s</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> contains only lowercase English letters.</li>
|
||||
</ul>
|
151
leetcode/problem/minimum-time-to-break-locks-i.html
Normal file
151
leetcode/problem/minimum-time-to-break-locks-i.html
Normal file
@@ -0,0 +1,151 @@
|
||||
<p>Bob is stuck in a dungeon and must break <code>n</code> locks, each requiring some amount of <strong>energy</strong> to break. The required energy for each lock is stored in an array called <code>strength</code> where <code>strength[i]</code> indicates the energy needed to break the <code>i<sup>th</sup></code> lock.</p>
|
||||
|
||||
<p>To break a lock, Bob uses a sword with the following characteristics:</p>
|
||||
|
||||
<ul>
|
||||
<li>The initial energy of the sword is 0.</li>
|
||||
<li>The initial factor <code><font face="monospace">x</font></code> by which the energy of the sword increases is 1.</li>
|
||||
<li>Every minute, the energy of the sword increases by the current factor <code>x</code>.</li>
|
||||
<li>To break the <code>i<sup>th</sup></code> lock, the energy of the sword must reach <strong>at least</strong> <code>strength[i]</code>.</li>
|
||||
<li>After breaking a lock, the energy of the sword resets to 0, and the factor <code>x</code> increases by a given value <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Your task is to determine the <strong>minimum</strong> time in minutes required for Bob to break all <code>n</code> locks and escape the dungeon.</p>
|
||||
|
||||
<p>Return the <strong>minimum </strong>time required for Bob to break all <code>n</code> locks.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">strength = [3,4,1], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Time</th>
|
||||
<th style="border: 1px solid black;">Energy</th>
|
||||
<th style="border: 1px solid black;">x</th>
|
||||
<th style="border: 1px solid black;">Action</th>
|
||||
<th style="border: 1px solid black;">Updated x</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">Nothing</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">Break 3<sup>rd</sup> Lock</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">Nothing</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">4</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">Break 2<sup>nd</sup> Lock</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">4</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">Break 1<sup>st</sup> Lock</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>The locks cannot be broken in less than 4 minutes; thus, the answer is 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">strength = [2,5,4], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Time</th>
|
||||
<th style="border: 1px solid black;">Energy</th>
|
||||
<th style="border: 1px solid black;">x</th>
|
||||
<th style="border: 1px solid black;">Action</th>
|
||||
<th style="border: 1px solid black;">Updated x</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">Nothing</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">Nothing</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
<td style="border: 1px solid black;">Break 1<sup>st</sup> Lock</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">Nothing</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">4</td>
|
||||
<td style="border: 1px solid black;">6</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">Break 2<sup>n</sup><sup>d</sup> Lock</td>
|
||||
<td style="border: 1px solid black;">5</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">5</td>
|
||||
<td style="border: 1px solid black;">5</td>
|
||||
<td style="border: 1px solid black;">5</td>
|
||||
<td style="border: 1px solid black;">Break 3<sup>r</sup><sup>d</sup> Lock</td>
|
||||
<td style="border: 1px solid black;">7</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>The locks cannot be broken in less than 5 minutes; thus, the answer is 5.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == strength.length</code></li>
|
||||
<li><code>1 <= n <= 8</code></li>
|
||||
<li><code>1 <= K <= 10</code></li>
|
||||
<li><code>1 <= strength[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
47
leetcode/problem/smallest-number-with-all-set-bits.html
Normal file
47
leetcode/problem/smallest-number-with-all-set-bits.html
Normal file
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <em>positive</em> number <code>n</code>.</p>
|
||||
|
||||
<p>Return the <strong>smallest</strong> number <code>x</code> <strong>greater than</strong> or <strong>equal to</strong> <code>n</code>, such that the binary representation of <code>x</code> contains only <span data-keyword="set-bit">set bits</span></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</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">7</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The binary representation of 7 is <code>"111"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 10</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">15</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The binary representation of 15 is <code>"1111"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</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">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The binary representation of 3 is <code>"11"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
54
leetcode/problem/transformed-array.html
Normal file
54
leetcode/problem/transformed-array.html
Normal file
@@ -0,0 +1,54 @@
|
||||
<p>You are given an integer array <code>nums</code> that represents a circular array. Your task is to create a new array <code>result</code> of the <strong>same</strong> size, following these rules:</p>
|
||||
For each index <code>i</code> (where <code>0 <= i < nums.length</code>), perform the following <strong>independent</strong> actions:
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i] > 0</code>: Start at index <code>i</code> and move <code>nums[i]</code> steps to the <strong>right</strong> in the circular array. Set <code>result[i]</code> to the value of the index where you land.</li>
|
||||
<li>If <code>nums[i] < 0</code>: Start at index <code>i</code> and move <code>abs(nums[i])</code> steps to the <strong>left</strong> in the circular array. Set <code>result[i]</code> to the value of the index where you land.</li>
|
||||
<li>If <code>nums[i] == 0</code>: Set <code>result[i]</code> to <code>nums[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the new array <code>result</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> Since <code>nums</code> is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,-2,1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1,1,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>nums[0]</code> that is equal to 3, If we move 3 steps to right, we reach <code>nums[3]</code>. So <code>result[0]</code> should be 1.</li>
|
||||
<li>For <code>nums[1]</code> that is equal to -2, If we move 2 steps to left, we reach <code>nums[3]</code>. So <code>result[1]</code> should be 1.</li>
|
||||
<li>For <code>nums[2]</code> that is equal to 1, If we move 1 step to right, we reach <code>nums[3]</code>. So <code>result[2]</code> should be 1.</li>
|
||||
<li>For <code>nums[3]</code> that is equal to 1, If we move 1 step to right, we reach <code>nums[0]</code>. So <code>result[3]</code> should be 3.</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,4,-1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[-1,-1,4]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>nums[0]</code> that is equal to -1, If we move 1 step to left, we reach <code>nums[2]</code>. So <code>result[0]</code> should be -1.</li>
|
||||
<li>For <code>nums[1]</code> that is equal to 4, If we move 4 steps to right, we reach <code>nums[2]</code>. So <code>result[1]</code> should be -1.</li>
|
||||
<li>For <code>nums[2]</code> that is equal to -1, If we move 1 step to left, we reach <code>nums[1]</code>. So <code>result[2]</code> should be 4.</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>
|
Reference in New Issue
Block a user