끄적끄적
Cobility Lesson3 - PermMissingElem 본문
Cobility Lesson3 - PermMissingElem
https://app.codility.com/demo/results/training4QR7VV-HFX/
Test results - Codility
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 arra
app.codility.com
문제
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)].
정답
def solution(A):
big=max(A)
s=sum(A)
s_key=0
for i in range(1,big+1):
s_key+=i
return s_key-s
결과


- > 누락 숫자가 맨 마지막일 확률을 생각 못 함, 빈 배열이 들어갈 확률을 생각 못 함
정답-Retry
def solution(A):
if len(A)>=1:
big=max(A)
s=sum(A)
s_key=0
for i in range(1,big+1):
s_key+=i
if s_key==s:
result= big+1
else:
result = s_key-s
else:
result=1
return result
결과-Retry


'Codility Lessons' 카테고리의 다른 글
| Codility Lesson4 - FrogRiverOne (0) | 2023.09.14 |
|---|---|
| Cobility Lesson3 - TapeEquilibrium (0) | 2023.09.14 |
| Codility Lesson3 - FrogJmp (0) | 2023.09.13 |
| Codility Lesson2 - OddOccurrencesInArray (0) | 2023.09.13 |
| Codility Lesson2 - CyclicRotation (0) | 2023.09.13 |