mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-12-17 09:47:44 +08:00
update
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>binary reflection</strong> of a <strong>positive</strong> integer is defined as the number obtained by reversing the order of its <strong>binary</strong> digits (ignoring any leading zeros) and interpreting the resulting binary number as a decimal.</p>
|
||||
|
||||
<p>Sort the array in <strong>ascending</strong> order based on the binary reflection of each element. If two different numbers have the same binary reflection, the <strong>smaller</strong> original number should appear first.</p>
|
||||
|
||||
<p>Return the resulting sorted array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [4,5,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[4,4,5]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Binary reflections are:</p>
|
||||
|
||||
<ul>
|
||||
<li>4 -> (binary) <code>100</code> -> (reversed) <code>001</code> -> 1</li>
|
||||
<li>5 -> (binary) <code>101</code> -> (reversed) <code>101</code> -> 5</li>
|
||||
<li>4 -> (binary) <code>100</code> -> (reversed) <code>001</code> -> 1</li>
|
||||
</ul>
|
||||
Sorting by the reflected values gives <code>[4, 4, 5]</code>.</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [3,6,5,8]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[8,3,6,5]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Binary reflections are:</p>
|
||||
|
||||
<ul>
|
||||
<li>3 -> (binary) <code>11</code> -> (reversed) <code>11</code> -> 3</li>
|
||||
<li>6 -> (binary) <code>110</code> -> (reversed) <code>011</code> -> 3</li>
|
||||
<li>5 -> (binary) <code>101</code> -> (reversed) <code>101</code> -> 5</li>
|
||||
<li>8 -> (binary) <code>1000</code> -> (reversed) <code>0001</code> -> 1</li>
|
||||
</ul>
|
||||
Sorting by the reflected values gives <code>[8, 3, 6, 5]</code>.<br />
|
||||
Note that 3 and 6 have the same reflection, so we arrange them in increasing order of original value.</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,219 @@
|
||||
<p>You are given a string <code>s</code> of length <code>n</code> consisting only of the characters <code>'A'</code> and <code>'B'</code>.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>queries</code> of length <code>q</code>, where each <code>queries[i]</code> is one of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>[1, j]</code>: <strong>Flip</strong> the character at index <code>j</code> of <code>s</code> i.e. <code>'A'</code> changes to <code>'B'</code> (and vice versa). This operation mutates <code>s</code> and affects subsequent queries.</li>
|
||||
<li><code>[2, l, r]</code>: <strong>Compute</strong> the <strong>minimum</strong> number of character deletions required to make the <strong>substring</strong> <code>s[l..r]</code> <strong>alternating</strong>. This operation does not modify <code>s</code>; the length of <code>s</code> remains <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong><span data-keyword="substring-nonempty">substring</span></strong> is <strong>alternating</strong> if no two <strong>adjacent</strong> characters are <strong>equal</strong>. A substring of length 1 is always alternating.</p>
|
||||
|
||||
<p>Return an integer array <code>answer</code>, where <code>answer[i]</code> is the result of the <code>i<sup>th</sup></code> query of type <code>[2, l, r]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "ABA", queries = [[2,1,2],[1,1],[2,0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>i</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>queries[i]</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>j</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>l</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>r</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong><code>s</code> before query</strong></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>s[l..r]</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong>Result</strong></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong>Answer</strong></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 1, 2]</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"BA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">Already alternating</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 1]</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">Flip <code>s[1]</code> from <code>'B'</code> to <code>'A'</code></td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 0, 2]</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"AAA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"AAA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">Delete any two <code>'A'</code>s to get <code>"A"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, the answer is <code>[0, 2]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "ABB", queries = [[2,0,2],[1,2],[2,0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,0]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>i</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>queries[i]</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>j</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>l</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>r</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong><code>s</code> before query</strong></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>s[l..r]</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong>Result</strong></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong>Answer</strong></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 0, 2]</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABB"</code></td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABB"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">Delete one <code>'B'</code> to get <code>"AB"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 2]</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABB"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">Flip <code>s[2]</code> from <code>'B'</code> to <code>'A'</code></td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 0, 2]</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">Already alternating</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, the answer is <code>[1, 0]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "BABA", queries = [[2,0,3],[1,1],[2,1,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>i</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>queries[i]</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>j</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>l</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>r</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong><code>s</code> before query</strong></th>
|
||||
<th align="center" style="border: 1px solid black;"><code><strong>s[l..r]</strong></code></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong>Result</strong></th>
|
||||
<th align="center" style="border: 1px solid black;"><strong>Answer</strong></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 0, 3]</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">3</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"BABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"BABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">Already alternating</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 1]</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"BABA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">Flip <code>s[1]</code> from <code>'A'</code> to <code>'B'</code></td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 1, 3]</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">3</td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"BBBA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;"><code>"BBA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">Delete one <code>'B'</code> to get <code>"BA"</code></td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, the answer is <code>[0, 1]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'A'</code> or <code>'B'</code>.</li>
|
||||
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code> or <code>3</code>
|
||||
<ul>
|
||||
<li><code>queries[i] == [1, j]</code> or,</li>
|
||||
<li><code>queries[i] == [2, l, r]</code></li>
|
||||
<li><code>0 <= j <= n - 1</code></li>
|
||||
<li><code>0 <= l <= r <= n - 1</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -0,0 +1,73 @@
|
||||
<p>You are given a <strong>circular</strong> array <code>balance</code> of length <code>n</code>, where <code>balance[i]</code> is the net balance of person <code>i</code>.</p>
|
||||
|
||||
<p>In one move, a person can transfer <strong>exactly</strong> 1 unit of balance to either their left or right neighbor.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of moves required so that every person has a <strong>non-negative</strong> balance. If it is impossible, return <code>-1</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>: You are guaranteed that <strong>at most</strong> 1 index has a <strong>negative</strong> balance initially.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">balance = [5,1,-4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal sequence of moves is:</p>
|
||||
|
||||
<ul>
|
||||
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [5, 0, -3]</code></li>
|
||||
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [4, 0, -2]</code></li>
|
||||
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [3, 0, -1]</code></li>
|
||||
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [2, 0, 0]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Thus, the minimum number of moves required is 4.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">balance = [1,2,-5,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal sequence of moves is:</p>
|
||||
|
||||
<ul>
|
||||
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [1, 1, -4, 2]</code></li>
|
||||
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, -3, 2]</code></li>
|
||||
<li>Move 1 unit from <code>i = 3</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, -2, 1]</code></li>
|
||||
<li>Move 1 unit from <code>i = 3</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, -1, 0]</code></li>
|
||||
<li>Move 1 unit from <code>i = 0</code> to <code>i = 1</code>, resulting in <code>balance = [0, 1, -1, 0]</code></li>
|
||||
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [0, 0, 0, 0]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Thus, the minimum number of moves required is 6.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">balance = [-3,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><strong></strong>It is impossible to make all balances non-negative for <code>balance = [-3, 2]</code>, so the answer is -1.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == balance.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= balance[i] <= 10<sup>9</sup></code></li>
|
||||
<li>There is at most one negative value in <code>balance</code> initially.</li>
|
||||
</ul>
|
||||
@@ -0,0 +1,68 @@
|
||||
<p>You are given a string <code>s</code> consisting of lowercase English words, each separated by a single space.</p>
|
||||
|
||||
<p>Determine how many vowels appear in the <strong>first</strong> word. Then, reverse each following word that has the <strong>same vowel count</strong>. Leave all remaining words unchanged.</p>
|
||||
|
||||
<p>Return the resulting string.</p>
|
||||
|
||||
<p>Vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "cat and mice"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"cat dna mice"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The first word <code>"cat"</code> has 1 vowel.</li>
|
||||
<li><code>"and"</code> has 1 vowel, so it is reversed to form <code>"dna"</code>.</li>
|
||||
<li><code>"mice"</code> has 2 vowels, so it remains unchanged.</li>
|
||||
<li>Thus, the resulting string is <code>"cat dna mice"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "book is nice"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"book is ecin"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The first word <code>"book"</code> has 2 vowels.</li>
|
||||
<li><code>"is"</code> has 1 vowel, so it remains unchanged.</li>
|
||||
<li><code>"nice"</code> has 2 vowels, so it is reversed to form <code>"ecin"</code>.</li>
|
||||
<li>Thus, the resulting string is <code>"book is ecin"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "banana healthy"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"banana healthy"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The first word <code>"banana"</code> has 3 vowels.</li>
|
||||
<li><code>"healthy"</code> has 2 vowels, so it remains unchanged.</li>
|
||||
<li>Thus, the resulting string is <code>"banana healthy"</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters and spaces.</li>
|
||||
<li>Words in <code>s</code> are separated by a <strong>single</strong> space.</li>
|
||||
<li><code>s</code> does <strong>not</strong> contain leading or trailing spaces.</li>
|
||||
</ul>
|
||||
@@ -0,0 +1,49 @@
|
||||
<p>You are given an integer <code>n</code>.</p>
|
||||
|
||||
<p>Return the <strong>largest <span data-keyword="prime-number">prime number</span></strong> less than or equal to <code>n</code> that can be expressed as the <strong>sum</strong> of one or more <strong>consecutive prime numbers</strong> starting from 2. If no such number exists, return 0.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 20</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">17</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The prime numbers less than or equal to <code>n = 20</code> which are consecutive prime sums are:</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p><code>2 = 2</code></p>
|
||||
</li>
|
||||
<li>
|
||||
<p><code>5 = 2 + 3</code></p>
|
||||
</li>
|
||||
<li>
|
||||
<p><code>17 = 2 + 3 + 5 + 7</code></p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>The largest is 17, so it is the answer.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only consecutive prime sum less than or equal to 2 is 2 itself.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 5 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,64 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An <strong>inversion</strong> is a pair of indices <code>(i, j)</code> from <code>nums</code> such that <code>i < j</code> and <code>nums[i] > nums[j]</code>.</p>
|
||||
|
||||
<p>The <strong>inversion count</strong> of a <strong><span data-keyword="subarray-nonempty">subarray</span></strong> is the number of inversions within it.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> inversion count among all <strong>subarrays</strong> of <code>nums</code> with length <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 = [3,1,2,5,4], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We consider all subarrays of length <code>k = 3</code> (indices below are relative to each subarray):</p>
|
||||
|
||||
<ul>
|
||||
<li><code>[3, 1, 2]</code> has 2 inversions: <code>(0, 1)</code> and <code>(0, 2)</code>.</li>
|
||||
<li><code>[1, 2, 5]</code> has 0 inversions.</li>
|
||||
<li><code>[2, 5, 4]</code> has 1 inversion: <code>(1, 2)</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The minimum inversion count among all subarrays of length <code>3</code> is 0, achieved by subarray <code>[1, 2, 5]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,3,2,1], k = 4</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There is only one subarray of length <code>k = 4</code>: <code>[5, 3, 2, 1]</code>.<br />
|
||||
Within this subarray, the inversions are: <code>(0, 1)</code>, <code>(0, 2)</code>, <code>(0, 3)</code>, <code>(1, 2)</code>, <code>(1, 3)</code>, and <code>(2, 3)</code>.<br />
|
||||
Total inversions is 6, so the minimum inversion count is 6.</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], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>All subarrays of length <code>k = 1</code> contain only one element, so no inversions are possible.<br />
|
||||
The minimum inversion count is therefore 0.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,67 @@
|
||||
<p>You are given an integer <code>num</code>.</p>
|
||||
|
||||
<p>A number <code>num</code> is called a <strong>Complete <span data-keyword="prime-number">Prime Number</span></strong> if every <strong>prefix</strong> and every <strong>suffix</strong> of <code>num</code> is <strong>prime</strong>.</p>
|
||||
|
||||
<p>Return <code>true</code> if <code>num</code> is a Complete Prime Number, otherwise return <code>false</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>prefix</strong> of a number is formed by the <strong>first</strong> <code>k</code> digits of the number.</li>
|
||||
<li>A <strong>suffix</strong> of a number is formed by the <strong>last</strong> <code>k</code> digits of the number.</li>
|
||||
<li>Single-digit numbers are considered Complete Prime Numbers only if they are <strong>prime</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">num = 23</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong></strong>Prefixes of <code>num = 23</code> are 2 and 23, both are prime.</li>
|
||||
<li>Suffixes of <code>num = 23</code> are 3 and 23, both are prime.</li>
|
||||
<li>All prefixes and suffixes are prime, so 23 is a Complete Prime Number and the answer is <code>true</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">num = 39</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">false</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Prefixes of <code>num = 39</code> are 3 and 39. 3 is prime, but 39 is not prime.</li>
|
||||
<li>Suffixes of <code>num = 39</code> are 9 and 39. Both 9 and 39 are not prime.</li>
|
||||
<li>At least one prefix or suffix is not prime, so 39 is not a Complete Prime Number and the answer is <code>false</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">num = 7</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">true</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>7 is prime, so all its prefixes and suffixes are prime and the answer is <code>true</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,131 @@
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<p>For each element <code>nums[i]</code>, you may perform the following operations <strong>any</strong> number of times (including zero):</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase <code>nums[i]</code> by 1, or</li>
|
||||
<li>Decrease <code>nums[i]</code> by 1.</li>
|
||||
</ul>
|
||||
|
||||
<p>A number is called a <strong>binary palindrome</strong> if its binary representation without leading zeros reads the same forward and backward.</p>
|
||||
|
||||
<p>Your task is to return an integer array <code>ans</code>, where <code>ans[i]</code> represents the <strong>minimum</strong> number of operations required to convert <code>nums[i]</code> into a <strong>binary palindrome</strong>.</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,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal set of operations:</p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;"><code>nums[i]</code></th>
|
||||
<th style="border: 1px solid black;">Binary(<code>nums[i]</code>)</th>
|
||||
<th style="border: 1px solid black;">Nearest<br />
|
||||
Palindrome</th>
|
||||
<th style="border: 1px solid black;">Binary<br />
|
||||
(Palindrome)</th>
|
||||
<th style="border: 1px solid black;">Operations Required</th>
|
||||
<th style="border: 1px solid black;"><code>ans[i]</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<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;">1</td>
|
||||
<td style="border: 1px solid black;">Already palindrome</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
<td style="border: 1px solid black;">10</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">11</td>
|
||||
<td style="border: 1px solid black;">Increase by 1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">4</td>
|
||||
<td style="border: 1px solid black;">100</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
<td style="border: 1px solid black;">11</td>
|
||||
<td style="border: 1px solid black;">Decrease by 1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, <code>ans = [0, 1, 1]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [6,7,12]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,0,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>One optimal set of operations:</p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;"><code>nums[i]</code></th>
|
||||
<th style="border: 1px solid black;">Binary(<code>nums[i]</code>)</th>
|
||||
<th style="border: 1px solid black;">Nearest<br />
|
||||
Palindrome</th>
|
||||
<th style="border: 1px solid black;">Binary<br />
|
||||
(Palindrome)</th>
|
||||
<th style="border: 1px solid black;">Operations Required</th>
|
||||
<th style="border: 1px solid black;"><code>ans[i]</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">6</td>
|
||||
<td style="border: 1px solid black;">110</td>
|
||||
<td style="border: 1px solid black;">5</td>
|
||||
<td style="border: 1px solid black;">101</td>
|
||||
<td style="border: 1px solid black;">Decrease by 1</td>
|
||||
<td style="border: 1px solid black;">1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">7</td>
|
||||
<td style="border: 1px solid black;">111</td>
|
||||
<td style="border: 1px solid black;">7</td>
|
||||
<td style="border: 1px solid black;">111</td>
|
||||
<td style="border: 1px solid black;">Already palindrome</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">12</td>
|
||||
<td style="border: 1px solid black;">1100</td>
|
||||
<td style="border: 1px solid black;">15</td>
|
||||
<td style="border: 1px solid black;">1111</td>
|
||||
<td style="border: 1px solid black;">Increase by 3</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, <code>ans = [1, 0, 3]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5000</code></li>
|
||||
<li><code><sup></sup>1 <= nums[i] <=<sup> </sup>5000</code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,67 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>hp</code> and two <strong>positive</strong> <strong>1-indexed</strong> integer arrays <code>damage</code> and <code>requirement</code>.</p>
|
||||
|
||||
<p>There is a dungeon with <code>n</code> trap rooms numbered from 1 to <code>n</code>. Entering room <code>i</code> reduces your health points by <code>damage[i]</code>. After that reduction, if your remaining health points are <strong>at least</strong> <code>requirement[i]</code>, you earn <strong>1 point </strong>for that room.</p>
|
||||
|
||||
<p>Let <code>score(j)</code> be the number of <strong>points</strong> you get if you start with <code>hp</code> health points and enter the rooms <code>j</code>, <code>j + 1</code>, ..., <code>n</code> in this order.</p>
|
||||
|
||||
<p>Return the integer <code>score(1) + score(2) + ... + score(n)</code>, the sum of scores over all starting rooms.</p>
|
||||
|
||||
<p><strong>Note</strong>: You cannot skip rooms. You can finish your journey even if your health points become non-positive.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">hp = 11, damage = [3,6,7], requirement = [4,2,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>score(1) = 2</code>, <code>score(2) = 1</code>, <code>score(3) = 0</code>. The total score is <code>2 + 1 + 0 = 3</code>.</p>
|
||||
|
||||
<p>As an example, <code>score(1) = 2</code> because you get 2 points if you start from room 1.</p>
|
||||
|
||||
<ul>
|
||||
<li>You start with 11 health points.</li>
|
||||
<li>Enter room 1. Your health points are now <code>11 - 3 = 8</code>. You get 1 point because <code>8 >= 4</code>.</li>
|
||||
<li>Enter room 2. Your health points are now <code>8 - 6 = 2</code>. You get 1 point because <code>2 >= 2</code>.</li>
|
||||
<li>Enter room 3. Your health points are now <code>2 - 7 = -5</code>. You do not get any points because <code>-5 < 5</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">hp = 2, damage = [10000,1], requirement = [1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>score(1) = 0</code>, <code>score(2) = 1</code>. The total score is <code>0 + 1 = 1</code>.</p>
|
||||
|
||||
<p><code>score(1) = 0</code> because you do not get any points if you start from room 1.</p>
|
||||
|
||||
<ul>
|
||||
<li>You start with 2 health points.</li>
|
||||
<li>Enter room 1. Your health points are now <code>2 - 10000 = -9998</code>. You do not get any points because <code>-9998 < 1</code>.</li>
|
||||
<li>Enter room 2. Your health points are now <code>-9998 - 1 = -9999</code>. You do not get any points because <code>-9999 < 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><code>score(2) = 1</code> because you get 1 point if you start from room 2.</p>
|
||||
|
||||
<ul>
|
||||
<li>You start with 2 health points.</li>
|
||||
<li>Enter room 2. Your health points are now <code>2 - 1 = 1</code>. You get 1 point because <code>1 >= 1</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= hp <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= n == damage.length == requirement.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= damage[i], requirement[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Find the absolute difference between:</p>
|
||||
|
||||
<ul>
|
||||
<li>the <strong>sum</strong> of the <code>k</code> <strong>largest</strong> elements in the array; and</li>
|
||||
<li>the <strong>sum</strong> of the <code>k</code> <strong>smallest</strong> elements in the array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an integer denoting this difference.</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,2,4], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <code>k = 2</code> largest elements are 4 and 5. Their sum is <code>4 + 5 = 9</code>.</li>
|
||||
<li>The <code>k = 2</code> smallest elements are 2 and 2. Their sum is <code>2 + 2 = 4</code>.</li>
|
||||
<li>The absolute difference is <code>abs(9 - 4) = 5</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 = [100], k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The largest element is 100.</li>
|
||||
<li>The smallest element is 100.</li>
|
||||
<li>The absolute difference is <code>abs(100 - 100) = 0</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
||||
@@ -0,0 +1,77 @@
|
||||
<p>You are given an <strong>undirected tree</strong> with <code>n</code> nodes, numbered from 0 to <code>n - 1</code>. It is represented by a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[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 tree.</p>
|
||||
|
||||
<p>You are also given an integer array <code>good</code> of length <code>n</code>, where <code>good[i]</code> is 1 if the <code>i<sup>th</sup></code> node is good, and 0 if it is bad.</p>
|
||||
|
||||
<p>Define the <strong>score</strong> of a <strong>subgraph</strong> as the number of good nodes minus the number of bad nodes in that subgraph.</p>
|
||||
|
||||
<p>For each node <code>i</code>, find the <strong>maximum</strong> possible score among all <strong>connected subgraphs</strong> that contain node <code>i</code>.</p>
|
||||
|
||||
<p>Return an array of <code>n</code> integers where the <code>i<sup>th</sup></code> element is the <strong>maximum</strong> score for node <code>i</code>.</p>
|
||||
|
||||
<p>A <strong>subgraph</strong> is a graph whose vertices and edges are subsets of the original graph.</p>
|
||||
|
||||
<p>A <strong>connected subgraph</strong> is a subgraph in which every pair of its vertices is reachable from one another using only its edges.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="Tree Example 1" src="https://assets.leetcode.com/uploads/2025/11/17/tree1fixed.png" style="width: 271px; height: 51px;" /></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1],[1,2]], good = [1,0,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Green nodes are good and red nodes are bad.</li>
|
||||
<li>For each node, the best connected subgraph containing it is the whole tree, which has 2 good nodes and 1 bad node, resulting in a score of 1.</li>
|
||||
<li>Other connected subgraphs containing a node may have the same score.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img alt="Tree Example 2" src="https://assets.leetcode.com/uploads/2025/11/17/tree2.png" style="width: 211px; height: 231px;" /></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[1,0],[1,2],[1,3],[3,4]], good = [0,1,0,1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,3,2,3,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Node 0: The best connected subgraph consists of nodes <code>0, 1, 3, 4</code>, which has 3 good nodes and 1 bad node, resulting in a score of <code>3 - 1 = 2</code>.</li>
|
||||
<li>Nodes 1, 3, and 4: The best connected subgraph consists of nodes <code>1, 3, 4</code>, which has 3 good nodes, resulting in a score of 3.</li>
|
||||
<li>Node 2: The best connected subgraph consists of nodes <code>1, 2, 3, 4</code>, which has 3 good nodes and 1 bad node, resulting in a score of <code>3 - 1 = 2</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<p><img alt="Tree Example 3" src="https://assets.leetcode.com/uploads/2025/11/17/tree3.png" style="width: 161px; height: 51px;" /></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1]], good = [0,0]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[-1,-1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>For each node, including the other node only adds another bad node, so the best score for both nodes is -1.</p>
|
||||
</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] = [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>good.length == n</code></li>
|
||||
<li><code>0 <= good[i] <= 1</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
||||
@@ -0,0 +1,66 @@
|
||||
<p>You are given two integer arrays, <code>technique1</code> and <code>technique2</code>, each of length <code>n</code>, where <code>n</code> represents the number of tasks to complete.</p>
|
||||
|
||||
<ul>
|
||||
<li>If the <code>i<sup>th</sup></code> task is completed using technique 1, you earn <code>technique1[i]</code> points.</li>
|
||||
<li>If it is completed using technique 2, you earn <code>technique2[i]</code> points.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer <code>k</code>, representing the <strong>minimum</strong> number of tasks that <strong>must</strong> be completed using technique 1.</p>
|
||||
|
||||
<p>You <strong>must</strong> complete <strong>at least</strong> <code>k</code> tasks using technique 1 (they do not need to be the first <code>k</code> tasks).</p>
|
||||
|
||||
<p>The remaining tasks may be completed using <strong>either</strong> technique.</p>
|
||||
|
||||
<p>Return an integer denoting the <strong>maximum total points</strong> you can earn.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">technique1 = [5,2,10], technique2 = [10,3,8], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">22</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We must complete at least <code>k = 2</code> tasks using <code>technique1</code>.</p>
|
||||
|
||||
<p>Choosing <code>technique1[1]</code> and <code>technique1[2]</code> (completed using technique 1), and <code>technique2[0]</code> (completed using technique 2), yields the maximum points: <code>2 + 10 + 10 = 22</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">technique1 = [10,20,30], technique2 = [5,15,25], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">60</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We must complete at least <code>k = 2</code> tasks using <code>technique1</code>.</p>
|
||||
|
||||
<p>Choosing all tasks using technique 1 yields the maximum points: <code>10 + 20 + 30 = 60</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">technique1 = [1,2,3], technique2 = [4,5,6], k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">15</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Since <code>k = 0</code>, we are not required to choose any task using <code>technique1</code>.</p>
|
||||
|
||||
<p>Choosing all tasks using technique 2 yields the maximum points: <code>4 + 5 + 6 = 15</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == technique1.length == technique2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= technique1[i], technique2[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= n</code></li>
|
||||
</ul>
|
||||
Reference in New Issue
Block a user