Gidhub BE Developer

Programmers - 제일 작은 수 제거하기

2018-07-24
goodGid

Problem

Problem URL : 제일 작은 수 제거하기


[1] Answer Code (18. 07. 24)


//[1]
vector<int> solution(vector<int> arr) {
    vector<int> answer;
    if (arr.size() == 1) {
        answer.push_back(-1);
        return answer;
    }
    int idx = 0;
    for(int i=1; i<arr.size(); i++){
        idx = arr[idx] > arr[i] ? i : idx;
    }
    for(int i=0; i<arr.size(); i++){
        if( i == idx ) continue;
        answer.push_back(arr[i]);
    }
    return answer;
}

// [2]
vector<int> solution(vector<int> arr) {
    vector<int> answer = arr;

    int nMin = *min_element(arr.begin(), arr.end());
    int pos = find(answer.begin(), answer.end(), nMin) - answer.begin();
    answer.erase(answer.begin() + pos);

    return answer.empty() ? vector<int>(1, -1) : answer;
}



[1] Answer Code (18. 07. 24)

  • [2] 굉장히 좋은 vector 사용법

Comments

Content