1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-04 15:01:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/最优激活顺序得到的最大总和(English) [maximum-total-from-optimal-activation-order].html
2025-08-10 21:35:14 +08:00

195 lines
8.9 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] &lt;= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</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;">0</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;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</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>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[1, 2]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<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;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:<strong></strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<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;">5</td>
<td align="center" style="border: 1px solid black;">0</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;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</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>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</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;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == value.length == limit.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= value[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= limit[i] &lt;= n</code></li>
</ul>