Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

사고쳤어요

[2024 KAKAO WINTER INTERNSHIP] 가장 많이 받은 선물 (C++) 본문

프로그래머스

[2024 KAKAO WINTER INTERNSHIP] 가장 많이 받은 선물 (C++)

kevinmj12 2024. 7. 16. 00:31

링크: https://school.programmers.co.kr/learn/courses/30/lessons/258712?language=cpp

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이

단순한 구현 문제이다. 입력으로 주어진 선물 기록을 반영하여 저장하고, 다음 달 선물을 어떻게 줄 것인지 간단히 계산하면 풀리는 문제이다.

코드

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

int giftCnt[50][50][2];
int giftScore[50];
int giftCntNextMonth[50];

int findIndex(string name, vector<string> friends){
    for (int i = 0; i < friends.size(); i++){
        if (name == friends[i]){
            return i;
        }
    }
}

int getGiftScore(int aIndex, int size){
    int aScore = 0;
    for (int i = 0; i < size; i++){
        aScore += giftCnt[aIndex][i][0] - giftCnt[aIndex][i][1];
    }
    return aScore;
}

int solution(vector<string> friends, vector<string> gifts) {
    int answer = 0;
    
    // 먼저 선물 기록을 저장
    for (int i = 0; i < gifts.size(); i++){
        string aName = "", bName = "";
        bool findSpace = false;
        for (int j = 0; j < gifts[i].length(); j++){
            if (gifts[i][j] == ' '){
                findSpace = true;
            }
            else if (!findSpace){
                aName += gifts[i][j];
            }
            else{
                bName += gifts[i][j];
            }
        }
        
        int aIndex = findIndex(aName, friends);
        int bIndex = findIndex(bName, friends);
        
        giftCnt[aIndex][bIndex][0]++;
        giftCnt[bIndex][aIndex][1]++;
    }
    
    // 먼저 선물 점수를 전부 계산
    for (int i = 0; i < friends.size(); i++){
        giftScore[i] = getGiftScore(i, friends.size());
    }
    
    // 다음 달 친구들 선물 반영
    for (int i = 0; i < friends.size()-1; i++){
        for (int j = i+1; j < friends.size(); j++){
            int aIndex = findIndex(friends[i], friends);
            int bIndex = findIndex(friends[j], friends);
            int aToB = giftCnt[aIndex][bIndex][0];
            int bToA = giftCnt[bIndex][aIndex][0];
            
            // a가 선물을 더 많이 준 경우
            if (aToB > bToA){
                giftCntNextMonth[aIndex]++;
            }
            // b가 선물을 더 많이 준 경우
            else if (bToA > aToB){
                giftCntNextMonth[bIndex]++;
            }
            // 선물을 준 횟수가 같은 경우
            else{
                if (giftScore[i] > giftScore[j]){
                    giftCntNextMonth[aIndex]++;
                }
                else if (giftScore[i] < giftScore[j]){
                    giftCntNextMonth[bIndex]++;
                }
            }
        }
    }
    // 정답
    for (int i = 0; i < friends.size(); i++){
        answer = max(answer, giftCntNextMonth[i]);
    }
    
    return answer;
}