신입 개발자 공부 과정

[프로그래머스] 완주하지 못한 선수 - Hash 본문

C++/공부

[프로그래머스] 완주하지 못한 선수 - Hash

Lewisjkim 2023. 12. 1. 12:13

알아두기

map.begin() - 첫 항목 위치 반환
map.find() - 지정된 Key와 같은 키를 포함하는 맵 내 요소의 위치를 가리키는 반복기를 반환
단, 지정된 Key와 같은 Key를 맵 내에서 찾지 못한다면 map.end()==마지막 요소 다음 위치 반환
map.insert(key값,value값) - 맵에 요소 등록
map[key값]-- or ++       - 해당 key값으로 등록된 value값 -- or ++

문제=

코드 =

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


string solution(vector<string> participant, vector<string> completion) {
	string answer = "";
	
	unordered_map<string, int> map;
	for (auto player : participant)
	{
		unordered_map<string, int>::iterator iter = map.begin();
		unordered_map<string, int>::iterator iterend = map.end();
		if (map.end() == map.find(player))//없는경우(find는 일치하는 항목이 없을 경우 end() 곧 마지막 요소 다음에 나오는 위치)
			map.insert(make_pair(player, 1));//전체 명단을 map에 value 1로 등록
		else//map에 기존 항복이 있을 경우(중복)
			map[player]++;//value를 1 더해준다
	}
	for (auto finisher : completion)
	{
		map[finisher]--;//완주 사람들을 map에서 value 값을 1씩 빼준다
	}
	for (auto player : participant)
	{
		if (map[player] > 0)
		{
			answer = player;
			break;
		}
	}
	return answer;
}

int main()
{
	vector<string> names{ "mislav", "stanko", "mislav", "ana" };
	vector<string> finisher{ "stanko", "ana", "mislav" };
	string answer = solution(names, finisher);
	cout << answer;
	
}

'C++ > 공부' 카테고리의 다른 글

C++ 처음부터 다시 공부하기 (진행 중)  (1) 2024.04.15
프로그래머스 의상 C++  (1) 2023.12.05
프로그래머스 0 떼기  (1) 2023.11.30
프로그래머스 하샤드 수 C++  (0) 2023.11.07
IMGUI 툴 디자인 하기  (0) 2023.07.16