그럴듯한 개발 블로그
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

프로그래머스

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

programmers.co.kr

카카오 문제들은 헷갈리는 부분들이 좀 많은 것 같다.

풀다 보니 상당히 드럽게 됐는데, 그래도 빠른 게 낫겠지...?

#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

string solution(vector<string> survey, vector<int> choices) {
    string answer = "";
    unordered_map<char, int>    map;
    
    map['R'] = 0; // 사전 순으로 빠른 애들을 key로 map을 만들어준다.
    map['C'] = 0;
    map['J'] = 0;
    map['A'] = 0;
    
    for (int i = 0; i < survey.size(); i++)
    {
        if (map.find(survey[i][0]) != map.end()) // 앞에꺼가 키
            map[survey[i][0]] -= (choices[i] - 4);
        else // 뒤에꺼가 키
            map[survey[i][1]] += (choices[i] - 4);
        // cout << map['R'] << ' ' << map['C'] << ' ' << map['J'] << ' ' << map['A'] << '\n';
    }
    
    if (map['R'] >= 0)
        answer.push_back('R');
    else
        answer.push_back('T');
    if (map['C'] >= 0)
        answer.push_back('C');
    else
        answer.push_back('F');
    if (map['J'] >= 0)
        answer.push_back('J');
    else
        answer.push_back('M');
    if (map['A'] >= 0)
        answer.push_back('A');
    else
        answer.push_back('N');
    return answer;
}

다른 사람의 풀이를 보니 매우 깔끔하게 푸신 분들이 많았다.

char MBTI[4][2] = {
    {'R','T'},
    {'C','F'},
    {'J','M'},
    {'A','N'}
};

 

 string seq[4] = {"RT", "CF", "JM", "AN"};

이런 식으로 처리하는 방법들이 있었다.  그리고 풀고나니 return이 string인데 vector인줄 알고 push_back을 썻다. string은 += 로도 가능합니다~

반응형
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!