-
프로그래머스 - 위장알고리즘 풀이/프로그래머스 2019. 12. 5. 02:18
문제 : https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장 | 프로그래머스
programmers.co.kr
풀이 :
<string,int> 해쉬맵을 만든 후 clothes의 사이즈 만큼 for문을 돌면서 값이 없다면 +1씩 추가해준다.
그러면 예제1을 예시로 m[handgear] 에는 2가 m[eyewear] 은 1이 저장된다.
m[handgear] = 2
m[eyewear] = 1
이제 handgear 의 경우의 수는 3가지 ( 착용 x, yellow hat, green turban )
eyewear의 경우의 수는 2가지 ( 착용 x, blue sunglasses ) 이므로
총 경우의 수는 6가지가 나올 수 있다. 이때 모두 착용하지 않는 경우의 수 1을 빼준다. 답은 총 5가지
코드 ( C++ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <string> #include <vector> #include <iostream> #include <map> using namespace std; int solution(vector<vector<string>> clothes) { map<string, int> m; int ans = 1; for (int i = 0; i < clothes.size(); ++i) { m[clothes[i][1]]++; } for (auto it = m.begin(); it != m.end(); ++it) { ans *= (it->second) + 1; } return ans - 1; } int main() { vector<vector<string>> c = { {"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear" }, { "green_turban", "headgear" }}; cout << solution(c) << "\n"; return 0; } '알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - H-Index (0) 2019.12.08 프로그래머스 - 숫자 야구 (0) 2019.12.06 프로그래머스 - 탑 (0) 2019.12.03 프로그래머스 - 디스크 컨트롤러 (0) 2019.12.01 프로그래머스 - 가장 큰 수 (0) 2019.12.01