[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
Reference
この問題について([Codility/Counting Elements] MissingInteger), 我々は、より多くの情報をここで見つけました https://velog.io/@chaegil15/CodilityTime-Complexity-MissingIntegerテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol