[Algorithm] EquiLeader

Codility’s Lessons: Lesson 8 Leader - EquiLeader

Thanks to this task, I have learned the average time complexity of python’s dict family. Unlike my initial expectation, it shows just O(1) for insertion operation. I thought the inner structure of it would be balanced tree such as the usual implementation of std::map in C++. [Reference]

Task Description

A non-empty array A consisting of N integers is given.

The leader of this array is the value that occurs in more than half of the elements of A.

An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], …, A[S] and A[S + 1], A[S + 2], …, A[N − 1] have leaders of the same value.

For example, given array A such that:

​ A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2

we can find two equi leaders:

  • 0, because sequences: (4) and (3, 4, 4, 4, 2) have the same leader, whose value is 4.
  • 2, because sequences: (4, 3, 4) and (4, 4, 2) have the same leader, whose value is 4.

The goal is to count the number of equi leaders.

Write a function:

def solution(A)

that, given a non-empty array A consisting of N integers, returns the number of equi leaders.

For example, given:

​ A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2

the function should return 2, as explained above.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].

My Answer

As seen in CPython implementation, defaultdict is a type consisting of Python dictionary object (PyDictObject) and default_factory. Aside from the running cost of default_factory, it has the same time complexity as dict (Stackoverflow, UCI ICS-46). defaultdict explicits Hash Table and shows just O(1) for insertion operation in average (Python Wiki).

More read: Everything about Python dict - Stackoverflow

from collections import defaultdict

def solution(A):
    # write your code in Python 3.6
    marker_l = defaultdict(lambda : 0)
    marker_r = defaultdict(lambda : 0)
    
    for i in range(len(A)): 
        marker_r[A[i]] += 1
    
    n_equi_leader = 0
    leader = A[0]
    for i in range(len(A)):
        marker_r[A[i]] -= 1
        marker_l[A[i]] += 1
        
        if i == 0 or marker_l[leader] < marker_l[A[i]]:
            leader = A[i]
            
        if (i+1) // 2 < marker_l[leader] and (len(A) - (i+1)) // 2 < marker_r[leader]:
            n_equi_leader += 1
            
    return n_equi_leader