|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../sell-diminishing-valued-colored-balls "Sell Diminishing-Valued Colored Balls") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [1649. Create Sorted Array through Instructions (Hard)](https://leetcode.com/problems/create-sorted-array-through-instructions "通过指令创建有序数组") |
| 13 | + |
| 14 | +<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> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li> |
| 18 | + <li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li> |
| 19 | +</ul> |
| 20 | + |
| 21 | +<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> |
| 22 | + |
| 23 | +<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> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong>Example 1:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> instructions = [1,5,6,2] |
| 30 | +<strong>Output:</strong> 1 |
| 31 | +<strong>Explanation:</strong> Begin with nums = []. |
| 32 | +Insert 1 with cost min(0, 0) = 0, now nums = [1]. |
| 33 | +Insert 5 with cost min(1, 0) = 0, now nums = [1,5]. |
| 34 | +Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6]. |
| 35 | +Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6]. |
| 36 | +The total cost is 0 + 0 + 0 + 1 = 1.</pre> |
| 37 | + |
| 38 | +<p><strong>Example 2:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input:</strong> instructions = [1,2,3,6,5,4] |
| 42 | +<strong>Output:</strong> 3 |
| 43 | +<strong>Explanation:</strong> Begin with nums = []. |
| 44 | +Insert 1 with cost min(0, 0) = 0, now nums = [1]. |
| 45 | +Insert 2 with cost min(1, 0) = 0, now nums = [1,2]. |
| 46 | +Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3]. |
| 47 | +Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6]. |
| 48 | +Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6]. |
| 49 | +Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6]. |
| 50 | +The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3. |
| 51 | +</pre> |
| 52 | + |
| 53 | +<p><strong>Example 3:</strong></p> |
| 54 | + |
| 55 | +<pre> |
| 56 | +<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2] |
| 57 | +<strong>Output:</strong> 4 |
| 58 | +<strong>Explanation:</strong> Begin with nums = []. |
| 59 | +Insert 1 with cost min(0, 0) = 0, now nums = [1]. |
| 60 | +Insert 3 with cost min(1, 0) = 0, now nums = [1,3]. |
| 61 | +Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3]. |
| 62 | +Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3]. |
| 63 | +Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3]. |
| 64 | +Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4]. |
| 65 | +Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4]. |
| 66 | +Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4]. |
| 67 | +Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4]. |
| 68 | +The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4. |
| 69 | +</pre> |
| 70 | + |
| 71 | +<p> </p> |
| 72 | +<p><strong>Constraints:</strong></p> |
| 73 | + |
| 74 | +<ul> |
| 75 | + <li><code>1 <= instructions.length <= 10<sup>5</sup></code></li> |
| 76 | + <li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li> |
| 77 | +</ul> |
| 78 | + |
| 79 | +### Related Topics |
| 80 | + [[Binary Indexed Tree](../../tag/binary-indexed-tree/README.md)] |
| 81 | + [[Segment Tree](../../tag/segment-tree/README.md)] |
| 82 | + [[Ordered Map](../../tag/ordered-map/README.md)] |
| 83 | + |
| 84 | +### Hints |
| 85 | +<details> |
| 86 | +<summary>Hint 1</summary> |
| 87 | +This problem is closely related to finding the number of inversions in an array |
| 88 | +</details> |
| 89 | + |
| 90 | +<details> |
| 91 | +<summary>Hint 2</summary> |
| 92 | +if i know the position in which i will insert the i-th element in I can find the minimum cost to insert it |
| 93 | +</details> |
0 commit comments