Tap here to switch tabs
Problem
Submissions

Problem: Create Sorted Array through Instructions

Try to solve the Create Sorted Array through Instructions problem.

Statement

Given an integer array instructions, you are tasked with constructing a sorted array nums by inserting elements from instructions one by one, starting from an empty array nums.

For each element instructions[i] from left to right, insert it into nums such that nums remains sorted in nondecreasing order. The cost of inserting each element is defined as the minimum of:

  • The number of elements currently in nums that are strictly less than instructions[i].

  • The number of elements currently in nums that are strictly greater than instructions[i].

Return the total cost to insert all elements into nums. Since the answer may be large, return it modulo 109+710^9 + 7.

Constraints:

  • 1≤1 \leq instructions.length ≤105\leq 10^5

  • 1≤1 \leq instructions[i] ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Create Sorted Array through Instructions

Try to solve the Create Sorted Array through Instructions problem.

Statement

Given an integer array instructions, you are tasked with constructing a sorted array nums by inserting elements from instructions one by one, starting from an empty array nums.

For each element instructions[i] from left to right, insert it into nums such that nums remains sorted in nondecreasing order. The cost of inserting each element is defined as the minimum of:

  • The number of elements currently in nums that are strictly less than instructions[i].

  • The number of elements currently in nums that are strictly greater than instructions[i].

Return the total cost to insert all elements into nums. Since the answer may be large, return it modulo 109+710^9 + 7.

Constraints:

  • 1≤1 \leq instructions.length ≤105\leq 10^5

  • 1≤1 \leq instructions[i] ≤105\leq 10^5