Given an integer array instructions
, you are asked to create a sorted array from the elements in instructions
. You start with an empty container nums
. For each element from left to right in instructions
, insert it into nums
. The cost of each insertion is the minimum of the following:
nums
that are strictly less than instructions[i]
.nums
that are strictly greater than instructions[i]
.For example, if inserting element 3
into nums = [1,2,3,5]
, the cost of insertion is min(2, 1)
(elements 1
and 2
are less than 3
, element 5
is greater than 3
) and nums
will become [1,2,3,3,5]
.
Return the total cost to insert all elements from instructions
into nums
. Since the answer may be large, return it modulo 109 + 7
Example 1:
Input: instructions = [1,5,6,2] Output: 1 Explanation: 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.
Example 2:
Input: instructions = [1,2,3,6,5,4] Output: 3 Explanation: 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]. Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3]. Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6]. Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6]. Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6]. The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
Example 3:
Input: instructions = [1,3,3,3,2,4,2,1,2] Output: 4 Explanation: Begin with nums = []. Insert 1 with cost min(0, 0) = 0, now nums = [1]. Insert 3 with cost min(1, 0) = 0, now nums = [1,3]. Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3]. Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3]. Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3]. Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4]. Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4]. Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4]. Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4]. The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
Constraints:
1 <= instructions.length <= 105
1 <= instructions[i] <= 105
This problem asks to calculate the total cost of inserting elements from the instructions
array into an initially empty sorted array nums
. The cost of each insertion is the minimum of the number of elements in nums
strictly less than the inserted element and the number of elements strictly greater than the inserted element. The solution leverages Binary Indexed Trees (BIT) for efficient calculation of smaller and larger elements.
A Binary Indexed Tree (also known as a Fenwick Tree) is a data structure that allows for efficient prefix sum queries and updates in O(log n) time. This makes it perfect for this problem.
Data Structure: We use a BIT to store the count of elements less than or equal to a given value. The maximum value in instructions
determines the size of the BIT.
Iteration: We iterate through instructions
. For each element x
:
x
(tree.query(x - 1)
).x
is the current index i
minus the number of elements less than or equal to x
. We take the minimum of these two counts as the cost for inserting x
.x
(tree.update(x, 1)
).Total Cost: The total cost is accumulated across all insertions, and the result is returned modulo 10^9 + 7
.
instructions
and m is the maximum value in instructions
. Each insertion involves two BIT queries and one update, all of which take O(log m) time.class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def update(self, x, v):
while x <= self.n:
self.c[x] += v
x += x & -x
def query(self, x):
s = 0
while x:
s += self.c[x]
x -= x & -x
return s
class Solution:
def createSortedArray(self, instructions):
m = max(instructions)
tree = BinaryIndexedTree(m)
ans = 0
mod = 10**9 + 7
for i, x in enumerate(instructions):
cost = min(tree.query(x - 1), i - tree.query(x))
ans = (ans + cost) % mod
tree.update(x, 1)
return ans
The code in other languages (Java, C++, Go, TypeScript) follows a very similar structure, adapting the syntax and data structures to the specific language. The core logic remains the same: using a BIT for efficient querying and updating. The Segment Tree solution offers the same time complexity but is generally less efficient in practice due to the overhead of managing the tree structure.