#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr) {
vector<int> answer;
int size= arr.size();
int min=arr[0];
for (int i=0; i<size; i++){
if (arr[i]<min){
min=arr[i];
}
}
if (size<=1){
answer.push_back(-1);
return answer;
}
else{
arr.erase(remove(arr.begin(), arr.end(), min), arr.end());
return arr;
}
}
정답 코드
배운점
c++ 벡터 특정 원소 지우는 방법
[C++] vector 특정 원소 지우기
vector에서 원소 하나를 삭제하는 시간복잡도는 O(N)이다. 값으로 삭제 vector v; 라고 선언된 벡터에서 특정한 원소를 지울 때 아래와 같이 할 수 있습니다. v.erase(remove(v.begin(), v.end(), 지우고 싶은 원
mangu.tistory.com
v.erase(remove(v.begin(), v.end(), 지우고 싶은 원소), v.end());
'CS > 알고리즘' 카테고리의 다른 글
[c++][프로그래머스] 바탕화면 정리 [실패] (0) | 2023.08.06 |
---|---|
[C++][프로그래머스] 실패율 [실패] (0) | 2023.08.06 |
[c++][프로그래머스] 푸드파이터 [성공] (0) | 2023.08.03 |
[c++] [프로그래머스]크기가 작은 문자열 [성공] (0) | 2023.08.02 |
[c++][프로그래머스] 성격 유형 검사하기_카카오 2022 인턴십 기출 (0) | 2023.08.01 |