Skip to content

Practice Create Sorted Array through Instructions

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1649
LeetCode Problem

Create Sorted Array through Instructions

Problem Statement

Process instructions from left to right, inserting each value into an initially empty sorted collection. Its insertion cost is the smaller of the number of existing elements strictly below it and the number strictly above it; equal values count toward neither side. Return the total cost modulo 10^9 + 7.

Source: Create Sorted Array through Instructions on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
instructions = [1,5,6,2]
Output
1

# Example 2

Input
instructions = [1,2,3,6,5,4]
Output
3

# Example 3

Input
instructions = [1,3,3,3,2,4,2,1,2]
Output
4

Constraints

1 <= instructions.length <= 10^5
1 <= instructions[i] <= 10^5

Related LeetCode Problems

Practice more problems similar to Create Sorted Array through Instructions.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.