하루 1문제 챌린지/Level1

2024 카카오 겨울 인턴십 가장 많이 받은 선물 C++

그린푸딩 2024. 2. 25. 01:29
728x90
반응형

 

 

#include <string>
#include <vector>
#include<sstream>
#include<map>
#include<iostream>

using namespace std;
//선물지수: 친구에게 준 - 받은  
//가장 많은 선물 주고받을 친구 선물수 

//선물지수 계산
map<string,int>received;
map<string,int>give;
map<string,int>total; 
//주고받은거 
map<pair<string,string>,int>gives;
int solution(vector<string> friends, vector<string> gifts) {
    int answer = 0;
    
    
    //주고받은거 정리 
    string a,b;
    
    for(int i=0;i<gifts.size();i++){
       stringstream ss(gifts[i]);
       ss>>a>>b; 
       give[a]++;
       received[b]++;
       gives[{a,b}]++; 
    }
    //주고받은 기록 -> 선물지수 
    
    for(int i=0;i<friends.size();i++){
        for(int j=i+1;j<friends.size();j++){
          
            string p=friends[i];
            string q=friends[j];
            if(gives[{p,q}] || gives[{q,p}]){
                if(gives[{p,q}]>gives[{q,p}]){ //p가 준게 더많음 
                    total[p]++; continue;   
                }
                else if(gives[{p,q}]<gives[{q,p}]){  
                    total[q]++; continue;   
                }
                                      
            }
            //주고받지 않거나 주고 받은 갯수 같을때 
            int p_gift=give[p]-received[p];
            int q_gift=give[q]-received[q];
            if(p_gift<q_gift){
                total[q]++;

            }              
            if(p_gift>q_gift){
                total[p]++;
            }
       
          }
    }
     int maxs=0;                
    for(int i=0;i<friends.size();i++){
        
        maxs=max(maxs,total[friends[i]]);
    }
    answer=maxs;
    return answer;
}
728x90
반응형