코딩문제/프로그래머스
1레벨 : 제일 작은 수 제거하기
Drill_Labito
2020. 7. 10. 18:32
stl 에 익숙해지기 위해, algorithm, vector를 사용하여 문제를 풀기로 함. 문제설명에 보이듯, 배열 인수가 하나이면, vector의 pop 을 시키고, -1을 push, 그렇지 않은 경우, 벡터를 하나 더 생성하여, 기존벡터(배열)를 복사한 후, 오름차순 정렬을 시키면, 이 복사한 배열의 0번째 값이 최소값이 된다. 이를 find 함수를 통해 위치를 찾은 후, 인자를 지워주면 된다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr) {
vector<int> tmp;
vector<int> tmp2;
tmp = arr;
tmp2 = tmp;
if (tmp.size() == 1) {
tmp.pop_back();
tmp.push_back(-1);
return tmp;
}
sort(tmp2.begin(), tmp2.end());
vector<int>::iterator iter;
iter = find(tmp.begin(), tmp.end(), tmp2[0]);
tmp.erase(iter);
return tmp;
}