끄적끄적

Codility Lesson4 - MissingInteger 본문

Codility Lessons

Codility Lesson4 - MissingInteger

kminx 2023. 9. 19. 13:49

Codility Lesson4 - MissingInteger

https://app.codility.com/demo/results/trainingZFTD5X-7DF/

 

Test results - Codility

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 =

app.codility.com

 

문제

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

 

정답

def solution(A):

    A.sort()
    pivot=1
    
    for i in range(len(A)):
        if A[i]==pivot:
            pivot+=1
    

    return pivot

 

결과

이전 문제에서 계속 timeout 문제가 발생했기에 그걸 고려하느라 간단히 생각할 수 있는 걸 오래 끌었음. 좀 더 간단하게 생각할 필요가 있음. 쉬운 문제인데 다른 사람 풀이 보기 전까진 생각해내지 못함

'Codility Lessons' 카테고리의 다른 글

Codility Lesson5 - CountDiv  (0) 2023.09.20
Codility Lesson5 - PassingCars  (0) 2023.09.20
Codility Lesson4 - MaxCounters  (0) 2023.09.18
Codility Lesson4 - PermCheck  (0) 2023.09.18
Codility Lesson4 - FrogRiverOne  (0) 2023.09.14