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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to build a sorted list by inserting numbers one at a time. The brute force way to find the cost is to look at every possible place to insert each number and see how much it costs to do so.
Here's how the algorithm would work step-by-step:
def create_sorted_array_brute_force(instructions):
sorted_array = []
total_cost = 0
for instruction_index in range(len(instructions)):
instruction = instructions[instruction_index]
minimum_cost = float('inf')
best_insertion_index = -1
for insertion_index in range(len(sorted_array) + 1):
# Try every possible index for insertion
temp_array = sorted_array[:]
temp_array.insert(insertion_index, instruction)
current_cost = 0
# Calculate insertion cost at current index
for check_index in range(len(temp_array)):
if check_index > insertion_index and temp_array[check_index] < instruction:
current_cost += 1
if current_cost < minimum_cost:
# Keep track of the minimum cost index
minimum_cost = current_cost
best_insertion_index = insertion_index
total_cost += minimum_cost
# Insert the new number into our array
if best_insertion_index == len(sorted_array):
sorted_array.append(instruction)
else:
sorted_array.insert(best_insertion_index, instruction)
return total_cost
Building the sorted array efficiently involves tracking how many numbers are smaller or larger than each new number as it's added. By using a special data structure, we can quickly find these counts and calculate the cost, ultimately minimizing the total expense of sorting.
Here's how the algorithm would work step-by-step:
import bisect
def create_sorted_array(instructions):
sorted_array = []
total_cost = 0
for instruction in instructions:
# Find how many elements are smaller
smaller_count = bisect.bisect_left(sorted_array, instruction)
# Find how many elements are larger
larger_count = len(sorted_array) - bisect.bisect_right(sorted_array, instruction)
# Choose the minimum cost
cost = min(smaller_count, larger_count)
total_cost += cost
# Insert into the sorted array to maintain order
bisect.insort(sorted_array, instruction)
return total_cost % (10**9 + 7)
Case | How to Handle |
---|---|
Empty instructions array | Return 0, as no instructions mean no cost. |
Instructions array with a single element | The cost will be 0, so return 0. |
Instructions array with all identical elements | The cost increases linearly since insertion position is predictable. |
Instructions array with monotonically increasing or decreasing elements | The cost increases linearly, close to optimal for sorted input. |
Instructions array containing large numbers (potential for integer overflow in cost calculation) | Use modular arithmetic (modulo 10^9 + 7) during cost accumulation to prevent integer overflow. |
Maximum-sized instructions array (performance considerations) | Use an efficient data structure like a Fenwick Tree or Segment Tree for insert position counting, ensuring logarithmic time complexity per instruction. |
Instructions array with extreme boundary values (e.g., very large or very small numbers) | Ensure the chosen data structure (e.g., Fenwick Tree or Segment Tree) can handle the range of input values without memory issues. |
Duplicate instructions requiring the same insertion point multiple times | Fenwick Tree will correctly update counts even with frequent duplicate insertions. |