mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>non-negative</strong> powers of <code>2</code>, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>In one operation, you must apply the following changes to the array:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any element of the array <code>nums[i]</code> such that <code>nums[i] > 1</code>.</li>
|
||||
<li>Remove <code>nums[i]</code> from the array.</li>
|
||||
<li>Add <strong>two</strong> occurrences of <code>nums[i] / 2</code> to the <strong>end</strong> of <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <em><strong>minimum number of operations</strong> you need to perform so that </em><code>nums</code><em> contains a <strong>subsequence</strong> whose elements sum to</em> <code>target</code>. If it is impossible to obtain such a subsequence, return <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,8], target = 7
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In the first operation, we choose element nums[2]. The array becomes equal to nums = [1,2,4,4].
|
||||
At this stage, nums contains the subsequence [1,2,4] which sums up to 7.
|
||||
It can be shown that there is no shorter sequence of operations that results in a subsequnce that sums up to 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,32,1,2], target = 12
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In the first operation, we choose element nums[1]. The array becomes equal to nums = [1,1,2,16,16].
|
||||
In the second operation, we choose element nums[3]. The array becomes equal to nums = [1,1,2,16,8,8]
|
||||
At this stage, nums contains the subsequence [1,1,2,8] which sums up to 12.
|
||||
It can be shown that there is no shorter sequence of operations that results in a subsequence that sums up to 12.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,32,1], target = 35
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that no sequence of operations results in a subsequence that sums up to 35.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 2<sup>30</sup></code></li>
|
||||
<li><code>nums</code> consists only of non-negative powers of two.</li>
|
||||
<li><code>1 <= target < 2<sup>31</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,121 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>receiver</code> of length <code>n</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>There are <code>n</code> players having a <strong>unique id</strong> in the range <code>[0, n - 1]</code> who will play a ball passing game, and <code>receiver[i]</code> is the id of the player who receives passes from the player with id <code>i</code>. Players can pass to themselves, <strong>i.e.</strong> <code>receiver[i]</code> may be equal to <code>i</code>.</p>
|
||||
|
||||
<p>You must choose one of the <code>n</code> players as the starting player for the game, and the ball will be passed <strong>exactly</strong> <code>k</code> times starting from the chosen player.</p>
|
||||
|
||||
<p>For a chosen starting player having id <code>x</code>, we define a function <code>f(x)</code> that denotes the <strong>sum</strong> of <code>x</code> and the <strong>ids</strong> of all players who receive the ball during the <code>k</code> passes, <strong>including repetitions</strong>. In other words, <code>f(x) = x + receiver[x] + receiver[receiver[x]] + ... + receiver<sup>(k)</sup>[x]</code>.</p>
|
||||
|
||||
<p>Your task is to choose a starting player having id <code>x</code> that <strong>maximizes</strong> the value of <code>f(x)</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> value of the function.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> <code>receiver</code> may contain duplicates.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th style="padding: 5px; border: 1px solid black;">Pass Number</th>
|
||||
<th style="padding: 5px; border: 1px solid black;">Sender ID</th>
|
||||
<th style="padding: 5px; border: 1px solid black;">Receiver ID</th>
|
||||
<th style="padding: 5px; border: 1px solid black;">x + Receiver IDs</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;"> </td>
|
||||
<td style="padding: 5px; border: 1px solid black;"> </td>
|
||||
<td style="padding: 5px; border: 1px solid black;"> </td>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">1</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">1</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">1</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">0</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">3</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">0</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">5</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">4</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">1</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">6</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> receiver = [2,0,1], k = 4
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The table above shows a simulation of the game starting with the player having id x = 2.
|
||||
From the table, f(2) is equal to 6.
|
||||
It can be shown that 6 is the maximum achievable value of the function.
|
||||
Hence, the output is 6.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th style="padding: 5px; border: 1px solid black;">Pass Number</th>
|
||||
<th style="padding: 5px; border: 1px solid black;">Sender ID</th>
|
||||
<th style="padding: 5px; border: 1px solid black;">Receiver ID</th>
|
||||
<th style="padding: 5px; border: 1px solid black;">x + Receiver IDs</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;"> </td>
|
||||
<td style="padding: 5px; border: 1px solid black;"> </td>
|
||||
<td style="padding: 5px; border: 1px solid black;"> </td>
|
||||
<td style="padding: 5px; border: 1px solid black;">4</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">1</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">4</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">3</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">7</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">3</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">9</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 5px; border: 1px solid black;">3</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">2</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">1</td>
|
||||
<td style="padding: 5px; border: 1px solid black;">10</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> receiver = [1,1,1,2,3], k = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> The table above shows a simulation of the game starting with the player having id x = 4.
|
||||
From the table, f(4) is equal to 10.
|
||||
It can be shown that 10 is the maximum achievable value of the function.
|
||||
Hence, the output is 10.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= receiver.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= receiver[i] <= n - 1</code></li>
|
||||
<li><code>1 <= k <= 10<sup>10</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given positive integers <code>n</code> and <code>target</code>.</p>
|
||||
|
||||
<p>An array <code>nums</code> is <strong>beautiful</strong> if it meets the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == n</code>.</li>
|
||||
<li><code>nums</code> consists of pairwise <strong>distinct</strong> <strong>positive</strong> integers.</li>
|
||||
<li>There doesn't exist two <strong>distinct</strong> indices, <code>i</code> and <code>j</code>, in the range <code>[0, n - 1]</code>, such that <code>nums[i] + nums[j] == target</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible sum that a beautiful array could have</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, target = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can see that nums = [1,3] is beautiful.
|
||||
- The array nums has length n = 2.
|
||||
- The array nums consists of pairwise distinct positive integers.
|
||||
- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.
|
||||
It can be proven that 4 is the minimum possible sum that a beautiful array could have.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, target = 3
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> We can see that nums = [1,3,4] is beautiful.
|
||||
- The array nums has length n = 3.
|
||||
- The array nums consists of pairwise distinct positive integers.
|
||||
- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.
|
||||
It can be proven that 8 is the minimum possible sum that a beautiful array could have.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, target = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can see, that nums = [1] is beautiful.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= target <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a string <code>moves</code> of length <code>n</code> consisting only of characters <code>'L'</code>, <code>'R'</code>, and <code>'_'</code>. The string represents your movement on a number line starting from the origin <code>0</code>.</p>
|
||||
|
||||
<p>In the <code>i<sup>th</sup></code> move, you can choose one of the following directions:</p>
|
||||
|
||||
<ul>
|
||||
<li>move to the left if <code>moves[i] = 'L'</code> or <code>moves[i] = '_'</code></li>
|
||||
<li>move to the right if <code>moves[i] = 'R'</code> or <code>moves[i] = '_'</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>distance from the origin</strong> of the <strong>furthest</strong> point you can get to after </em><code>n</code><em> moves</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> moves = "L_RL__R"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> moves = "_R__LL_"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> moves = "_______"
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= moves.length == n <= 50</code></li>
|
||||
<li><code>moves</code> consists only of characters <code>'L'</code>, <code>'R'</code> and <code>'_'</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user