Sorted Array python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bisect | |
class SortedArray: | |
def __init__(self, array): | |
self.array = sorted(array) | |
self.d = len(array) | |
def add(self, x): | |
bisect.insort(self.array, x) | |
def remove(self, x): | |
del self.array[bisect.bisect_left(self.array, x)] | |
def median(self): | |
d = self.d | |
if d % 2 == 1: | |
return self.array[d//2] | |
else: | |
return (self.array[(d-1)//2] + self.array[(d+1)//2])/2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment