코딩 테스트/프로그래머스 코딩테스트

[프로그래머스] 추억 점수

ingus kinematics 2025. 3. 8. 13:17

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

#include <string>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

vector<int> solution(vector<string> name, vector<int> yearning, vector<vector<string>> photo) {
    vector<int> answer;
    
    std::map<string, int> name_point;
    
    // name, yearning 값을 map에 복사
    for(int i=0; i<name.size(); i++){
        name_point.insert({name[i], yearning[i]});
    }
    
    // 사진 갯수 탐색
    for(int i=0; i<photo.size(); i++){
        int score_yearning = 0;
        // 사진 속 인물들 탐색
        for(int j=0; j<photo[i].size(); j++){
            string person_in_picture = photo[i][j];
            
            // map 자료형 데이터 안에 있는지 찾기
            if(name_point.find(person_in_picture) != name_point.end()){
                // cout << "사진 속 사람 = " << person_in_picture << endl;
                // cout << "그리움 점수 = " << name_point[person_in_picture] << endl;
                
                score_yearning += name_point[person_in_picture];
            }
        }
        answer.push_back(score_yearning);
    }    
    
    // printf("Code Execution!\n");
    
    
    return answer;
}

 

요즘 chat gpt로 코드 짜다보니 코드 짜는 실력이 상당히 낮아졌습니다..

그런 기념으로 오랜만에 프로그래머스 코딩 테스트 시작.

 

<map 사용법>

map<key, value> 변수이름;

 

map 자료형에 데이터 삽입할 때 → insert 함수 사용, insert({변수1, 변수2})

key 값으로 데이터가 존재하는지 안하는지 확인하고 싶을 때, find 함수로 접근하기

만약 데이터가 존재하면 find(key)의 결과는 end()을 반환한다.