구현(Implementation)

프로그래머스 Level1 > 제일 작은 수 제거하기

cepiloth 2018. 6. 14. 18:16
반응형

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


1. 문제 요약

주어진 배열에서 가장 작은 수를 제외 하고 반환 하는 문제.


2. 알고리즘

주어진 배열에서 가장 작은 수를 찾는다.

작은 수를 제외하고 배열에 담아서 반환 한다.


주어진 배열의 크기가 1 이면 -1 을 담아 반환 한다.


3. 코드

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
31
32
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <functional>
 
using namespace std;
 
vector<int> solution(vector<int> arr) {
    vector<int> answer;
    const int size = arr.size();
    if(size == 1) {
        answer.push_back(-1);
        return answer;
    }
    
    int cand = 100000000;
    for(int i=0; i<size; i++) {
        if(arr[i] < cand) {
            cand = arr[i];
        }
    }
    
    for(int i=0; i<size; i++) {
        if(arr[i] == cand) {
            continue;
        }
        
        answer.push_back(arr[i]);
    }
    return answer;
}
cs

반응형