Leetcode: 1649. Create Sorted Array through Instructions

Problem Statement

from sortedcontainers import SortedList
from typing import List


class Solution:
    def createSortedArray(self, instructions: List[int]) -> int:
        MOD = 10**9 + 7
        N = len(instructions)
        s = SortedList()
        ans = 0
        for i in instructions:
            l = s.bisect_left(i)
            r = len(s) - s.bisect_left(i + 1)
            ans = (ans + min(l, r)) % MOD
            s.add(i)
        return ans