본문 바로가기

알고리즘&자료구조/Codility

[codility] MissingInteger

Task description

This is a demo task.

Write a function:

def solution(A)

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.

Given A = [1, 2, 3], the function should return 4.

Given A = [−1, −3], the function should return 1.

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..1,000,000].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

 

O(N) or O(N * log(N))

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    s1 = set([num if num > 0 else 0 for num in A])
    
    s2 = (num+1 for num in range(len(s1)+1))
    return min(set(s2).difference(s1))
    pass

'알고리즘&자료구조 > Codility' 카테고리의 다른 글

[codility] MaxCounters  (0) 2021.09.13
[codility] PermCheck  (0) 2021.09.02
[codility] FrogRiverOne  (0) 2021.09.02
[codility] TapeEquilibrium  (0) 2021.09.02
[codility] PermMissingElem  (0) 2021.09.02