Notice
Recent Posts
Recent Comments
Today
Total
01-26 00:02
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ] ํŠœํ”Œ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ] ํŠœํ”Œ

WonderJay 2022. 7. 11. 12:05
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/64065

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

1.  ์ž…๋ ฅ๋ฌธ์ž์—ด์ด "{{1,2},{1,2,3}}" ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ, ๋‹ค๋ฃจ๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ '{'  , ',' , '}' ๊ณผ ๊ฐ™์€ ๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ '  ' ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ์—ˆ๋‹ค.

2.  ๊ทธ๋ฆฌ๊ณ  stringstream ์„ ์ด์šฉํ•˜์—ฌ ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋ฌธ์ž๋ฅผ ํŒŒ์‹ฑํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์ˆซ์ž๊ฐ€ ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•ด count ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

3.  ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•˜๋Š” ์ด์œ ๋Š”,  ๋งŒ์•ฝ์— ์ž…๋ ฅ ๋ฌธ์ž์—ด์ด 

  • {{2}, {2, 1}, {2, 1, 3}, {2, 1, 3, 4}}

์™€ ๊ฐ™์ด ์ฃผ์–ด์ง€๋ฉด ๋Œ€์‘ํ•˜๋Š” ํŠœํ”Œ์€ {2,1,3,4} ์ด๊ณ ,  ์ž…๋ ฅ ๋ฌธ์ž์—ด์—์„œ 2๋Š” 4๋ฒˆ, 1์€ 3๋ฒˆ, 3์€ 2๋ฒˆ, 4๋Š” 1๋ฒˆ ๋‚˜ํƒ€๋‚œ๋‹ค. 

์ฆ‰, ์ž…๋ ฅ ๋ฌธ์ž์—ด์„ ์ฐจ๋ก€๋Œ€๋กœ vector ์— ๋ฐ›์•„๋“ค์ธ ๋‹ค์Œ, ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ฑฐ ๊ฐ๊ฐ์˜ ์ˆซ์ž๊ฐ€ ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด ํŠœํ”Œ์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

[C++]

#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    int count[100000+1] = {0};
    for(int i = 0 ; i < s.size(); i ++)
    {
        if(s[i] == '{' || s[i] == '}') s[i] = ' ';
        if(s[i] == ',')
            s[i] = ' ';
    }
    
    istringstream ss;
    ss.str(s);
    string buf = "";
    while(ss >> buf)
    {
        answer.push_back(stoi(buf));
        count[stoi(buf)]++;
    }
    sort(answer.begin(), answer.end());
    answer.erase(unique(answer.begin(), answer.end()), answer.end());
    sort(answer.begin(), answer.end(), [&](int a, int b)
         {
            return count[a] > count[b]; 
         });
    
    
    return answer;
}

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments