Python beats 97% O(logn) insert and O(1) median finding 3 lines total using bisect

  • 0

    Bisect lib in python helps keep a sorted array and insertion costs o(logn) because it implements a binary search on a sorted array. Finding median is just o(1) because we already know the array.

    def __init__(self): = []
    def addNum(self, num):
        bisect.insort_left(, num)
    def findMedian(self):
        return float([len(]) if len( else ([len(][len(]) / float(2) if len( > 0 else None

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.