mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-07 00:11:41 +08:00
add scripts and problemset
This commit is contained in:
64
算法题/create-sorted-array-through-instructions.html
Normal file
64
算法题/create-sorted-array-through-instructions.html
Normal file
@@ -0,0 +1,64 @@
|
||||
<p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
|
||||
|
||||
|
||||
|
||||
<ul>
|
||||
|
||||
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
|
||||
|
||||
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> instructions = [1,5,6,2]
|
||||
|
||||
<strong>Output:</strong> 1
|
||||
|
||||
<strong>Explanation:</strong> Begin with nums = [].
|
||||
|
||||
Insert 1 with cost min(0, 0) = 0, now nums = [1].
|
||||
|
||||
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
|
||||
|
||||
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
|
||||
|
||||
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
|
||||
|
||||
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
|
||||
|
||||
<strong>Output:</strong> 3
|
||||
|
||||
<strong>Explanation:</strong> Begin with nums = [].
|
||||
|
||||
Insert 1 with cost min(0, 0) = 0, now nums = [1].
|
||||
|
||||
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
|
||||
|
Reference in New Issue
Block a user