프로그래머스 - 위장

2022. 8. 26. 23:08코딩 공부 연습

반응형

해시 사용을 좀 잘해야겠단 필요성을 느꼈다. unordered set을 이용해서 풀다가 좀 많이 해맸다. 결국 그래도 해냈다고 생각했는데 아니었네!

#include <string>
#include <vector>
#include <iostream>
#include <math.h>
#include <unordered_set>
using namespace std;

int arr[33];
int solution(vector<vector<string>> clothes) {
    int answer = 1;
    unordered_set<string> us;
    int clothes_cnt = 0;
    for(int i =0;i< clothes.size(); i++)
    {
        if (us.find(clothes[i][1]) == us.end())
        {
            us.insert(clothes[i][1]);
            clothes_cnt++;
        }
        else
        {
            int tmp = 0;
            for (auto x : us)
            {
                if ( x == clothes[i][1])
                    arr[tmp]++;
                tmp++;
            }
        }
    }
    for(int i = 0; i < clothes_cnt; i++)
        answer *= (arr[i]+2);
    return answer-1;
}

원래는 unordered set에 pair로 개수를 새주는 친구를 넣으려고 했는데 pair가 안되더라. 그래서 그냥 일단 집어넣고, 만약 find에서 찾지 못한 녀석이면 그 index를 찾아서 따로 만든 배열 개수를 증가시켜주었다. 마지막으로 다 1씩 증가시켜준후 곱하고, 아무것도 안입으면 잡혀가기 때문에 1감소시켜 주었다. 근데 틀렸다ㅎ 

뭔가 해시를 안쓰면 더 쉽게 풀릴거 같긴한데, 이왕 이렇게 된거 해시를 써서 해볼 생각이다. 왜 저 코드가 틀렸는지는 모르겠다. 억울하다!

 

고민의흔적 1.. 11시25분

걍 set으로 바꿈.. unordered set 이라 혹시 for 순회할 때 순서가 바뀌는건 아닐까 하는 걱정에..

#include <string>
#include <vector>
#include <iostream>
#include <math.h>
//#include <ordered_set>
#include <set>
using namespace std;

int arr[33];
int solution(vector<vector<string>> clothes) {
    int answer = 1;
    set<string> us;
    int clothes_cnt = 0;
    for(int i =0;i< clothes.size(); i++)
    {
        if (us.find(clothes[i][1]) == us.end())
        {
            us.insert(clothes[i][1]);
            clothes_cnt++;
        }
        else
        {
            int tmp = 0;
            for (auto x : us)
            {
                if ( x == clothes[i][1])
                    arr[tmp]++;
                tmp++;
            }
        }
    }
    for(int i = 0; i < clothes_cnt; i++)
        answer *= (arr[i]+2);
    return answer-1;
}

40점따리에서 60점 따리가 되었다.

뭔가 아예 틀리게 생각하는게 있는거 같당.. 반례가 보고싶어 ㅜ

 

마지막.. 11시 50분.. 저 위에서 pair로 하려다 실패했다고 말했는데 저 때 map을 쓰면 되겠다는 당연한 생각을 못했다. map으로 바꾸고 맞췄다.. 그 이런 stl 제공 해시 들을 잘 사용하지 못하는것 같다. 최대한 많이 사용해보면 분명 도움이 되겠다 싶다!

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

using namespace std;

int arr[33];
int solution(vector<vector<string>> clothes) {
    int answer = 1;
    map<string , int> m;
    int clothes_cnt = 0;
    for(int i =0;i< clothes.size(); i++)
    {
        if (m.find(clothes[i][1])== m.end())
        {
            m.insert(pair<string, int>(clothes[i][1],0));
            m[clothes[i][1]]++;    
        }
        else m[clothes[i][1]]++;
    }
    for (auto iter : m)
        answer *= (iter.second +1);
    return answer-1;
}

아이디어는 해시 때랑 똑같은데 이번에는 map에 개수를 세주는 인덱스가 있어서 바로 이용했다.

 

끝..

 

24485등

'코딩 공부 연습' 카테고리의 다른 글

프로그래머스 - 구명 보트  (0) 2022.08.30
프로그래머스 - 방문 길이  (0) 2022.08.30
백준 7785 - 회사에 있는 사람  (0) 2022.08.26
백준 - 5014 스타트링크  (0) 2022.08.24
프로그래머스 - 네트워크  (0) 2022.08.24