[Codility/Counting Elements] MissingInteger
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
Author And Source
이 문제에 관하여([Codility/Counting Elements] MissingInteger), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@chaegil15/CodilityTime-Complexity-MissingInteger저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)