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

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

 

프로그래머스

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

programmers.co.kr

문자 별로 cnt 값을 map에 저장해 두고 target을 순회하면서 더해준다.

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

using namespace std;

vector<int> solution(vector<string> keymap, vector<string> targets) {
    vector<int> answer;
    unordered_map<char, int>   map;
    
    for (int i = 0; i < keymap.size(); i++)
    {
        for (int j = 0; j < keymap[i].size(); j++)
        { // map에 문자를 key로 횟수를 value로 넣어준다
            if (map.find(keymap[i][j]) == map.end() || (map[keymap[i][j]] > j + 1))
                map[keymap[i][j]] = j + 1;
            // cout << "key: " << keymap[i][j] << " value: " << map[keymap[i][j]] << '\n';
        }
    }
    for (int i = 0; i < targets.size(); i++)
    {
        int sum = 0;
        
        for (int j = 0; j < targets[i].size(); j++)
        {
            if (map.find(targets[i][j]) == map.end())
            { // 없는 값일때 처리
                sum = -1;
                break ;
            }
            sum += map[targets[i][j]];
        }
        answer.push_back(sum);
    }   
    return answer;
}

이건 좀 잘 푼 것 같다. 나 자신 칭찬해~

이번 문제부터 크롬 확장프로그램인 백준 허브로 깃헙에 자동 업로드 되게 해 놓았다. 문제 링크도 있고 런타임도 나와서 아주 간편하다. 

https://github.com/donghyun1998/algorithm/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/unrated/160586.%E2%80%85%EB%8C%80%EC%B6%A9%E2%80%85%EB%A7%8C%EB%93%A0%E2%80%85%EC%9E%90%ED%8C%90

반응형
profile

그럴듯한 개발 블로그

@donghyk2

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