[C++] 프로그래머스 가장 많이 받은 선물

2024. 1. 11. 12:15·Algorithm/Programmers
#include <string>
#include <vector>
#include <map>
#include <sstream>

using namespace std;

int solution(vector<string> friends, vector<string> gifts) {
    int answer = 0;
    map<string, int> giftPoint;
    map<string, int> presentCount;
    map<string, map<string, int>> giftLog;

    for (int i = 0; i < friends.size(); ++i)
    {
        giftPoint.insert({ friends[i], 0 });
    }

    for (int i = 0; i < friends.size(); ++i)
    {
        //선물 내역 초기화와 자기 자신은 제거
        giftLog[friends[i]] = giftPoint;
        giftLog[friends[i]].erase(friends[i]);
    }

    for (int i = 0; i < gifts.size(); ++i)
    {
        stringstream ss(gifts[i]);
        string first, second;

        ss >> first >> second;

        //선물 지수
        giftPoint[first] += 1;
        giftPoint[second] -= 1;

        //1대1 선물 지수 저장
        giftLog[first][second] += 1;
        giftLog[second][first] -= 1;
    }

    for (auto me : giftLog)
    {
        for (auto other : me.second)
        {
            string myName = me.first;
            string otherName = other.first;

            if (other.second > 0)
            {
                presentCount[myName] += 1;
            }
            else if (other.second == 0)
            {
                if (giftPoint[myName] > giftPoint[otherName])
                {
                    presentCount[myName] += 1;
                }
            }

            if (answer < presentCount[myName])
            {
                answer = presentCount[myName];
            }
        }
    }


    return answer;
}

 

풀이

1. 선물 지수, 선물을 주고 받은 기록, 선물 받을 개수를 저장할 변수를 저장해준다.

2. 선물을 주고 받은 기록에서 선물을 누가 많이 줬는가 > 같거나 서로 안줬다면 누가 선물 지수가 높은가 순으로 비교하며 선물 개수를 추가해준다.

3. 제일 높은 선물개수를 추가해준다.

 

 

친구들의 수가 적어서 오래 걸리지 않았지만 더 좋은 방법을 찾아서 효율적으로 풀 수 있을 것 같아서 아쉽다. 더 좋은 방법을 모색해봐야겠다. map에서 자기 자신을 제거하거나 아니면 나중에 찾을 때 자기 자신을 배제하거나 하는 방법중에 뭐가 더 좋을려나 더 생각해봐야겠다. 선물에 관한 변수들을 저장할 방법이 더 좋은 방법이 있을 것 같다.

저작자표시 (새창열림)

'Algorithm > Programmers' 카테고리의 다른 글

[C++] 프로그래머스 최댓값과 최솟값  (0) 2024.02.15
[C++] 프로그래머스 과제 진행하기  (1) 2024.02.09
[C++] 프로그래머스 미로 탈출 명령어  (1) 2024.01.03
[C++] 프로그래머스 거리두기 확인하기  (0) 2023.12.29
[C++] 프로그래머스 2016년  (0) 2022.03.05
'Algorithm/Programmers' 카테고리의 다른 글
  • [C++] 프로그래머스 최댓값과 최솟값
  • [C++] 프로그래머스 과제 진행하기
  • [C++] 프로그래머스 미로 탈출 명령어
  • [C++] 프로그래머스 거리두기 확인하기
chanheess
chanheess
'왜' 그렇게 했는가?에 대한 생각으로 공부 및 작업의 저장관리
  • chanheess
    왜 그렇게 생각했는가?
    chanheess
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Backend Programming
      • Game Programming
        • Unreal
        • DirectX
      • C++
        • Memo
        • Basic
        • Effective Modern
      • Algorithm
        • Memo
        • Baekjoon
        • Programmers
        • HackerRank, LeetCode
      • Data Structure
      • Design Pattern
      • Etc
        • Memo
        • Daily Log
        • Book
  • 최근 글

  • 최근 댓글

  • 태그

    dfs
    c++ 기초 플러스
    알고리즘
    spring
    SpringSecurity
    위클리 챌린지
    프로그래머스
    JWT
    Java
    티스토리챌린지
    JPA
    dp
    백준
    오블완
  • hELLO· Designed By정상우.v4.10.0
chanheess
[C++] 프로그래머스 가장 많이 받은 선물
상단으로

티스토리툴바