[Codility/Counting Elements] MissingInteger

코라닝·2021년 7월 14일
0

Codility Lessons

목록 보기
9/10

MissingInteger

Task Discription

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].

Solution

from bisect import bisect_right
def solution(A):
    # write your code in Python 3.6
    A.sort()
    arr = A[bisect_right(A,0):]
    if arr == [] or arr[0] > 1:
        return 1
    for i,j in zip(arr, arr[1:]):
        if j-i > 1:
            return i + 1
    return arr[-1] + 1

0개의 댓글