Problem solution · Python

Create Sorted Array Through Instructions

Create Sorted Array Through Instructions: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Direct simulation
Source
Kamyu LeetCode Solutions
Length
100 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Create Sorted Array Through Instructions, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 100 lines of Python from the credited upstream file create-sorted-array-through-instructions.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeCreate Sorted Array Through Instructions · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogm)# Space: O(m) class BIT(object):  # 0-indexed.    def __init__(self, n):        self.__bit = [0]*(n+1)  # Extra one for dummy node.     def add(self, i, val):        i += 1  # Extra one for dummy node.        while i < len(self.__bit):            self.__bit[i] += val            i += (i & -i)     def query(self, i):        i += 1  # Extra one for dummy node.        ret = 0        while i > 0:            ret += self.__bit[i]            i -= (i & -i)        return ret class Solution(object):    def createSortedArray(self, instructions):        """        :type instructions: List[int]        :rtype: int        """        MOD = 10**9 + 7        bit = BIT(max(instructions))        result = 0        for i, inst in enumerate(instructions):            inst -= 1            result += min(bit.query(inst-1), i-bit.query(inst))            bit.add(inst, 1)        return result % MOD  # Time:  O(nlogn)# Space: O(n)import itertoolsclass Solution_TLE(object):    def createSortedArray(self, instructions):        """        :type instructions: List[int]        :rtype: int        """        MOD = 10**9 + 7        def smallerMergeSort(idxs, start, end, counts):            if end - start <= 0:  # The size of range [start, end] less than 2 is always with count 0.                return 0             mid = start + (end - start) // 2            smallerMergeSort(idxs, start, mid, counts)            smallerMergeSort(idxs, mid + 1, end, counts)            r = start            tmp = []            for i in xrange(mid+1, end + 1):                # Merge the two sorted arrays into tmp.                while r <= mid and idxs[r][0] < idxs[i][0]:                    tmp.append(idxs[r])                    r += 1                tmp.append(idxs[i])                counts[idxs[i][1]] += r - start            while r <= mid:                tmp.append(idxs[r])                r += 1            # Copy tmp back to idxs            idxs[start:start+len(tmp)] = tmp                def largerMergeSort(idxs, start, end, counts):            if end - start <= 0:  # The size of range [start, end] less than 2 is always with count 0.                return 0             mid = start + (end - start) // 2            largerMergeSort(idxs, start, mid, counts)            largerMergeSort(idxs, mid + 1, end, counts)            r = start            tmp = []            for i in xrange(mid+1, end + 1):                # Merge the two sorted arrays into tmp.                while r <= mid and idxs[r][0] <= idxs[i][0]:                    tmp.append(idxs[r])                    r += 1                if r <= mid:                    tmp.append(idxs[i])                counts[idxs[i][1]] += mid - r + 1            while r <= mid:                tmp.append(idxs[r])                r += 1            # Copy tmp back to idxs            idxs[start:start+len(tmp)] = tmp         idxs = []        smaller_counts, larger_counts = [[0] * len(instructions) for _ in xrange(2)]        for i, inst in enumerate(instructions):            idxs.append((inst, i))        smallerMergeSort(idxs[:], 0, len(idxs)-1, smaller_counts)        largerMergeSort(idxs, 0, len(idxs)-1, larger_counts)        return sum(min(s, l) for s, l in itertools.izip(smaller_counts, larger_counts)) % MOD 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗