## Problem:

*Difficulty: Easy

Problem:

ctci-find-the-running-median-english

Approach:

Since the constraints is so high, we need a way to insert and sort the array (or list) that has the complexity equal or lower than O(logn). With n loop and each loop costs O(logn) we have the complexity O(nlogn), which is acceptable. Using linked list and binary search is one of the possible ways. But If you know Python, this problem can be solved in 10 lines of code, using insort method from bisect module.

Implementation: Python 3

import bisect n = int(input()) l = [] for _ in range(n): ai = int(input()) bisect.insort(l, ai) if len(l) % 2 == 0: print('%.1f' % ((l[len(l)//2-1]+l[len(l)//2])/2)) else: print('%.1f' % (l[len(l)//2]))

Advertisements

## Reply