Codility ‘PrefixSet’ 2010 Alpha Solution | MartinKysel.com
Given a table A of N integers from 0 to N-1 calculate the smallest such index P, that that {A[0],…,A[N-1]} = {A[0],…,A[P]}.
https://codility.com/demo/take-sample-test/alpha2010/
http://codesays.com/2014/solution-to-alpha2010-prefix-set-by-codility/
Read full article from Codility ‘PrefixSet’ 2010 Alpha Solution | MartinKysel.com
Given a table A of N integers from 0 to N-1 calculate the smallest such index P, that that {A[0],…,A[N-1]} = {A[0],…,A[P]}.
https://codility.com/demo/take-sample-test/alpha2010/
A non-empty zero-indexed array A consisting of N integers is given. The first covering prefix of array A is the smallest integer P such that 0≤P<N and such that every value that occurs in array A also occurs in sequence A[0], A[1], ..., A[P].
For example, the first covering prefix of the following 5−element array A:
A[0] = 2
A[1] = 2
A[2] = 1
A[3] = 0
A[4] = 1
is 3, because sequence [ A[0], A[1], A[2], A[3] ] equal to [2, 2, 1, 0], contains all values that occur in array A.
def
solution(A):
seen
=
set
()
smallest_prefix_idx
=
0
for
idx
in
xrange
(
len
(A)):
if
A[idx]
not
in
seen:
seen.add(A[idx])
smallest_prefix_idx
=
idx
return
smallest_prefix_idx
def solution(A):
alphabet = set()
for element in A:
alphabet.add(element)
for index in xrange(len(A)):
alphabet.discard(A[index])
if len(alphabet) == 0:
return index
each element of array A is an integer within the range [0..N−1]”2
3
4
5
6
7
8
9
10
|
def solution(A):
meet = [False]* len(A)
result = -1
for index in xrange(len(A)):
if meet[A[index]] == False:
result = index
meet[A[index]] = True
return result
|