Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 추천 시스템
- 협업 필터링
- selenium
- 머신러닝
- 부스트캠프
- Cosine-similarity
- wordcloud
- 분산 시스템
- 코테
- 웹스크래핑
- 백준
- 시각화
- 딥러닝
- Tensor
- 데이터 엔지니어링
- TF-IDF
- pytorch
- 파이썬
- 프로그래머스
- 웹크롤링
- codingtest
- 데이터
- 코딩테스트
- 추천시스템
- 알고리즘
- recommendation system
- SGD
- Overfitting
- coursera
- Python
Archives
- Today
- Total
개발자식
[Codility Lesson4] Counting Elements_MissingInteger 본문
문제 : MissingInteger
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
나의 코드
- Detected time complexity: O(N) or O(N * log(N))
- 마이너스로만 이루어진 경우 먼저 확인 -> return 1
- 리스트를 순회하면서 마이너스 값은 넘어가게 설정
- 값이 1인 경우는 특별히 확인하는 변수 설정 (1이 없다면 어떤 수가 와도 1 return)
- 나머지 경우는 이전 값과 1 차이가 안난다면 이전 값 +1 return
- 위에 해당하지 않는 경우 (ex 1,2,3,4,,,)는 리스트 마지막 값 +1
def solution(A):
temp=list(set(A))
temp.sort()
if max(A) <=0:
return 1
check=False
for i in range(len(temp)):
if temp[i]<=0:
continue
if temp[i]==1:
check=True
continue
if not check:
return 1
if temp[i]-temp[i-1] !=1:
return temp[i-1]+1
return temp[-1]+1
정답 코드
- Detected time complexity: O(N) or O(N * log(N))
def solution(A):
A.sort()
min = 1
for i in A:
if i == min:
min += 1
return min
깔끔하군,,,,
'Algorithm > Codility' 카테고리의 다른 글
[Codility Lesson4] MaxCounters (0) | 2022.06.30 |
---|---|
[Codility Lesson4] PermCheck (0) | 2022.06.30 |
[Codility_Lesson 4] FrogRiverOne (0) | 2022.06.30 |
[Codility Lesson17] Dynamic programming_NumberSolitaire (0) | 2022.06.29 |
[Codility Lesson3] Time Complexity_ TapeEquilibrium (0) | 2022.06.29 |
Comments