Solution to Equi-Leader by codility

24 Jan

Question: http://codility.com/demo/take-sample-test/equi_leader

Question Name: EquiLeader

A variant of the previous question.

22 Replies to “Solution to Equi-Leader by codility

  1. Here’s my C solution :

  2. Adding my Java Solution 100/100
    https://codility.com/demo/results/demoZSME5G-RXN/

  3. Well, again, I was numb to recognize the fact equileader exists if and only if it is the leader of the sequence. Fortunately, codility is kind this time and allows space complexity to be O(N). This gives me the opportunity to apply the old hashtable trick here. An extra vector is needed to store the leader-end-here. And then scan the sequence backward to find the leader starting from N-1 to 1, so we can compare the backward leader with the leader-end-here saved in the vector.
    I have to admit my solution is silly though…

  4. Sheng, alternatively, you can detect leader by comparing candidate_count > 0, if it is true, then candidate is definitely a leader, otherwise there is no leader in array.

  5. You are right: Python might be slow, but it really makes my code shorter…Well, comparing with the dummy solution I submitted last year ^_^

  6. It is amazing how programmers approach the same problem differently.
    here is my solution:
    https://codility.com/demo/results/trainingUQ5UYH-QTT/

    Thanks Sheng for this nice blog. 👍

  7. my solution(A):

  8. My solution in Python, with comments:

Leave a Reply to Sheng Cancel reply

Your email address will not be published. Required fields are marked *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!