문제링크
https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/
PermMissingElem coding task - Learn to Code - Codility
Find the missing element in a given permutation.
app.codility.com
풀이
def solution(A):
l = len(A)
if l == 0:
return 1
else:
return sum(range(1, l+2)) - sum(A)
처음에는 정렬을 한 후 요소를 2개씩 묶은 후 차이가 1이 아니면 빠진 요소를 찾을 수 있을 것이라 생각하고 작성했는데, 더 간단한 방법이 합을 이용하는 것이었다.
빠진 요소의 길이를 추가한만큼의 합에서 주어진 array의 합을 빼면 어떤 요소가 빠졌는지 나온다.
삽질하다 풀이를 발견해서 다행이다.
'Problem Solving > Codility' 카테고리의 다른 글
| [Codility Python] lesson 3 Time Complexity - TapeEquilibrium (0) | 2022.07.07 |
|---|---|
| [Codility Python] lesson 3 Time Complexity - FrogJmp (0) | 2022.07.07 |
| [Codility Python] lesson 2 Arrays - OddOccurrencesInArray (0) | 2022.07.07 |
| [Codility Python] lesson 2 Arrays - CyclicRotation (0) | 2022.07.07 |
| [Codility Python] lesson 1 Iterations - BinaryGap (0) | 2022.07.03 |