Create Sorted Array through Instructions Problem
Description
LeetCode Problem 1649.
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:
- The number of elements currently in
numsthat are strictly less thaninstructions[i]. - The number of elements currently in
numsthat are strictly greater thaninstructions[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 10^9 + 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 <= 10^51 <= instructions[i] <= 10^5
Solution
Approach: Binary Indexed Tree over the Values
Before inserting instructions[i], the container holds the i earlier numbers. We need two counts among them:
less: how many are strictly smaller thanx;greater: how many are strictly larger thanx, which isiminus how many are<= x.
Both are prefix counts over the values. Values are at most 105, so a binary indexed tree (Fenwick tree) indexed by value works: inserting x adds 1 at position x, and "how many are <= x" is a prefix sum. Both take O(log M) time.
So for each x: less = count(x - 1), greater = i - count(x), add min(less, greater) to the answer, then insert x. Equal values are in neither count, which is why greater uses count(x) and not count(x - 1).
The total can reach about 2.5 · 109, more than a 32-bit integer holds, so add it up in a 64-bit integer and take it modulo 10^9 + 7 at the end.
Complexity: O(n log M) time, where M is the largest value. O(M) extra space for the tree.
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def add(self, x):
while x <= self.n:
self.c[x] += 1
x += x & -x
def count_upto(self, x):
"""How many inserted values are <= x."""
s = 0
while x > 0:
s += self.c[x]
x -= x & -x
return s
class Solution:
def createSortedArray(self, instructions: List[int]) -> int:
tree = BinaryIndexedTree(max(instructions))
ans = 0
for i, x in enumerate(instructions): # i values are already inserted
less = tree.count_upto(x - 1)
greater = i - tree.count_upto(x)
ans += min(less, greater)
tree.add(x)
return ans % (10**9 + 7)