오랜만에 1레벨쪽을 보니까 4 문제가 추가되어 있어서 풀어보았습니다.

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. Map을 이용해 각 컬럼 명을 Key, 인덱스를 Value로 하여 매핑

2. 범위 기반 for문을 이용해 조건(ext 컬럼의 값이 val_ext보다 작은)에 맞는 데이터를 answer 벡터에 추가

3. sort_by 컬럼에 해당하는 인덱스의 값을 기준으로 answer 벡터를 오름차순으로 정렬

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

using namespace std;

map<string, int> m = {{"code", 0}, {"date", 1}, {"maximum", 2}, {"remain", 3}};

int idx;

bool cmp(const vector<int> &a, const vector<int> &b) {
    return a[idx] < b[idx];
}

vector<vector<int>> solution(vector<vector<int>> data, string ext, int val_ext, string sort_by) {
    vector<vector<int>> answer;
    
    for(auto d : data) {
        if(data[m[ext]] < val_ext)
            answer.push_back(d);
    }
    
    idx = m[sort_by];
    
    sort(answer.begin(), answer.end(), cmp);
    
    return answer;
}

+ Recent posts