알고리즘/프로그래머스 1단계

프로그래머스 - 추억 점수 - C++

게임만드는학생 2024. 7. 17. 14:23

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

 

프로그래머스

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

programmers.co.kr

 

#include <string>
#include <vector>
#include <map>
using namespace std;

vector<int> solution(vector<string> name, vector<int> yearning, vector<vector<string>> photo) {
    vector<int> answer;
    
    map<string,int> m;
    
    for(int i=0;i<name.size();i++)
    {
        m.insert({name[i],yearning[i]});
    }
    
    for(int i=0;i<photo.size();i++)
    {
        int score=0;
        for(int j=0;j<photo[i].size();j++)
        {
            score+=m[photo[i][j]];
        }
        answer.push_back(score);
    }
    
    return answer;
}

 

각 이름과 그에대한 그리움 점수가 배열로 주어지면 

사진 하나에 대한 그리움 점수를 계산하여 리턴해주는 문제이다. 

 

주어진 이름가 점수에 대한 배열을 map에 저장하였다. 

그리고 이중포문을 통해 각 사진당 score를 맵에서 찾아 answer에 저장한다.