File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
week4/priority-queues/task1 Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ ### Dynamic median
2+
3+ Design a data type that supports:
4+
5+ - insert in logarithmic time
6+ - find-the-median in constant time
7+ - remove-the-median in logarithmic time
8+
9+ If the number of keys in the data type is even, find/remove the lower median.
Original file line number Diff line number Diff line change 1+ # Design a data type that supports:
2+
3+ # - insert in logarithmic time
4+ # - find-the-median in constant time
5+ # - remove-the-median in logarithmic time
6+
7+ # If the number of keys in the data type is even, find/remove the lower median.
8+
9+ class MedianStruct
10+ attr_reader :array
11+
12+ def initialize ( array )
13+ @array = array . sort
14+ end
15+
16+ def insert ( n )
17+ array << n
18+ array . sort!
19+ end
20+
21+ def median
22+ array [ array . size / 2 ]
23+ end
24+
25+ def remove_median
26+ array . delete_at ( array . size / 2 )
27+ end
28+ end
You can’t perform that action at this time.
0 commit comments