Algorithm
[프로그래머스] 완주하지 못한 선수
일등하이
2023. 1. 18. 15:37
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/42576?language=cpp
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
중복을 허용 한다고 해서 multimap을 사용했지만 반례가 있는듯
다음 코드는 실패
#include<iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
string solution(vector<string> participant, vector<string> completion);
int main()
{
vector<string> participant = { "mislav", "stanko", "mislav", "ana" };
vector<string> completion = { "stanko", "ana", "mislav" };
string name = solution(participant, completion);
cout << name;
return 0;
}
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
std::multimap <string, int> map;
for (string name : participant) {
map.insert({ name, 0 });
}
for (string name : completion) {
//cout << name << "\n";
auto it = map.find(name);
if ((it->first == name)) {
it->second++;
}
}
for (pair<string, int> pair : map) {
/*cout << pair.first << " , " << pair.second << "\n";*/
if (pair.second == 0) {
answer = pair.first;
break;
}
}
return answer;
}
다시풀어보기
- 그래도 Hash 문제이고 개념을 이해하는 것이 중요하니 동일한 문제를 Hash로 풀어보자.
- Hash를 사용하더라도 결국 우리가 원하는 것은 Participant는 있고 Completion에는 없는 한 명을 찾는 것이다.
c++
#include <iostream>
#include <string>
#include <vector>
#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)
{
if (map.end() == map.find(player))
map.insert(make_pair(player, 1));
else
map[player]++;
}
for (auto player : completion)
map[player]--;
for (auto player : participant)
if (map[player] > 0)
{
answer = player;
break;
}
return answer;
}
int main(void)
{
vector<string> part = { "leo", "kiki", "eden" };
vector<string> comp = { "eden", "kiki" };
cout << solution(part, comp);
return 0;
}
c#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _4458
{
class Program
{
static void Main(string[] args)
{
//completion의 길이는 participant의 길이보다 1 작습니다.
//참가자 중에는 동명이인이 있을 수 있습니다.
string[] participant = { "marina", "josipa", "nikola", "vinko", "filipa" };
string[] completion = { "josipa", "filipa", "marina", "nikola" };
Console.WriteLine(Solution(participant, completion));
}
public static string Solution(string[] participant, string[] completion)
{
string answer = "";
var dic = new Dictionary<string, int>();
for (int i = 0; i < participant.Length; i++) {
if (dic.ContainsKey(participant[i]))
{
dic[participant[i]]++;
}
else {
dic.Add(participant[i], 1);
}
}
for (int i = 0; i < completion.Length; i++) {
dic[completion[i]]--;
}
//foreach (var pair in dic) {
// Console.WriteLine("{0}:{1}", pair.Key, pair.Value);
//}
answer = dic.Where(x => x.Value > 0).Select(x => x.Key).FirstOrDefault();
return answer;
}
}
}
풀이
https://coding-grandpa.tistory.com/85
[프로그래머스] 완주하지 못한 선수 문제 풀이(해시 Lv. 1) - 파이썬 Python
0. 동일 유형 문제 [프로그래머스] 완주하지 못한 선수 (해시 Lv. 1) [프로그래머스] 전화번호 목록 (해시 Lv. 2) [프로그래머스] 위장 (해시 Lv. 2) [프로그래머스] 베스트 앨범 (해시 Lv. 3) Youtube 영상으
coding-grandpa.tistory.com
반응형