Programmers/Level 1

C++ / 프로그래머스 / 제일 작은 수 제거하기

GitHubSeob 2021. 9. 13. 19:15

문제

https://programmers.co.kr/learn/courses/30/lessons/12935

 

코딩테스트 연습 - 제일 작은 수 제거하기

정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1

programmers.co.kr

문제풀이

 

 

코드

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr) {
    vector<int> answer;
    int min_num = *min_element(arr.begin(), arr.end());
    if (arr.size() == 1) answer.push_back(-1);
    else {
        bool visit = false;
        for (int idx = 0; idx < arr.size(); ++idx)
            if(arr[idx]==min_num&&!visit) visit=true;
            else answer.push_back(arr[idx]);
    }
    return answer;
}