Beats 92.8% | All hail Python Libraries | Binary Search
Please UpvoteCode
# Please Upvote

# Code
```python3 []
class Solution:
    def resultArray(self, nums: List[int]) -> List[int]:
        arr1 = [nums[0]]
        arr2 = [nums[1]]
        heap1 = SortedList([nums[0]])
        heap2 = SortedList([nums[1]])

        for x in nums[2:]:
            l1 = len(heap1)
            l2 = len(heap2)
            pos1 = heap1.bisect_right(x)
            pos2 = heap2.bisect_right(x)
            gc1 = l1 - pos1
            gc2 = l2 - pos2
            
            if gc1 > gc2:
                arr1.append(x)
                heap1.add(x)
            elif gc1 < gc2:
                arr2.append(x)
                heap2.add(x)
            else:
                if len(arr1)>len(arr2):
                    arr2.append(x)
                    heap2.add(x)
                else:
                    arr1.append(x)
                    heap1.add(x)
        return arr1+arr2



```
['Array', 'Simulation', 'Python3']