코딩 테스트/C++

프로그래머스, C++) Lv 2. 의상

나무늘보섬 2025. 10. 24. 16:54

문제

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

 

프로그래머스

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

programmers.co.kr

 

 

 

  • 해시 문제
  • unordered_map을 통해 key에 대한 value를 업데이트
  • 이미 key가 있는 경우, 자동적으로 value가 업데이트 됨.
  • set을 이용하려 했으나, key가 중복될 우려가 있어 불가
더보기

#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;


/*
의상 종류(key) : 의상 가짓수(value)
*/
int solution(vector<vector<string>> clothes) {
    unordered_map<string,int> cnt;
    for (auto &it : clothes) cnt[it[1]]++;

    long long ways = 1;
    for (auto &p : cnt) ways *= (p.second + 1);
    return (int)(ways - 1);
}