일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- box-sizing
- 포토샵
- Codility
- 수학
- CSS
- Gap
- Photoshop
- skt membership
- 상태
- transform
- 미디어 쿼리
- SK바이오사이언스
- 알고리즘
- JSX
- spring
- c
- float
- REM
- stl
- 소수
- 강화학습
- 확률
- Javascript
- 통신사할인
- react
- grid
- c++
- 반응형 웹
- pandas
Archives
- Today
- Total
sliver__
Codility : Lesson 4 (Counting Elem) - PermCheck 본문
728x90
안녕하세요~~
디벨롭퍼입니다~~~

https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/
PermCheck coding task - Learn to Code - Codility
Check whether array A is a permutation.
app.codility.com
***********************************************************************
풀어보실 분들을 위해서 링크 걸어두었습니다.
***********************************************************************
주어진 문제는 아래와 같습니다.
N개의 숫자가 담겨있는 A배열에서
배열이 연속적인 숫자를
모두 포함하고 있는지 찾는 문제입니다.
주어진 벡터를 전부 순회해서 가장 큰 값을 찾고
값들을 set에 넣었을 때,
가장 큰 값만큼 set에 들어있어야 하니
비교를 하면 되겠다고 생각이 들더군요.
아래는 제출한 코드입니다.
// you can use includes, for example:
// #include <algorithm>
#include <set>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int ret = 0;
int max = -1;
bool duplicate = false;
std::set<int> s;
for(std::vector<int>::iterator iter=A.begin(); iter!=A.end(); ++iter)
{
if(max < *iter) max = *iter;
if(s.find(*iter) != s.end())
{
duplicate = true;
break;
}
else
{
s.insert(*iter);
}
}
if(duplicate) ret = 0;
else if((int)s.size() == max) ret=1;
return ret;
}
이 문제에서 예외처리 해야할 점이 하나있는데요,
중복된 숫자가 있을 경우 permutation이 아닌조건 입니다.
이 경우를 따로 처리를 해줘야 하더라구요.
1일 1문제
역시 꾸준히는 정말 어려운 것 같아요 ㅠㅠ
그럼 이만~~

728x90
'CS > Codility' 카테고리의 다른 글
Codility:Lesson5(Prefix Sums) - Counting Div (0) | 2021.11.16 |
---|---|
Codility:Lesson5(Prefix Sums) - PassingCars (0) | 2021.11.16 |
Codility : Lesson 4 (Counting Elements) - FrogRiverOne (0) | 2021.09.28 |
Codility : Lesson 3 (Time complexity) - TapeEquilibrium (0) | 2021.09.27 |
Codility : Lesson 3 (Time complexity) - PermMissingElem (0) | 2021.09.26 |
Comments