티스토리 뷰

알고리즘

코딜리티 PermMissingElem

killog 2020. 10. 3. 23:57
반응형

엣지를 내가 스스로 생각해야한다는 것을 깨닫게 해준 문제이다.

N 이 0일수 있다는 것은 좀 문제가 오바인것 같은데

이거에 맞춰야한다.

양끝단을 항상 생각하자.

 

 

문제

An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.

Your goal is to find that missing element.

Write a function:

def solution(A)

that, given an array A, returns the value of the missing element.

For example, given array A such that:

A[0] = 2 A[1] = 3 A[2] = 1 A[3] = 5

the function should return 4, as it is the missing element.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [0..100,000];
  • the elements of A are all distinct;
  • each element of array A is an integer within the range [1..(N + 1)].

    Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or dis

 

 

def solution(A):
    # write your code in Python 3.6
    A.sort()
    if len(A)>=1:
        may_maxi=A[-1]
        may_sum= sum(range(A[-1]+1))
        if may_sum>sum(A):
            return may_sum -sum(A)
        else:
            return may_maxi+1
            
    else:
        return 1
        # N 이 0임수 있음을 인지( 한번 틀림)
반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함