[Programmers] Lv2. [3์ฐจ] ํŒŒ์ผ๋ช… ์ •๋ ฌ | C++

2023. 6. 24. 16:36ใ†Coding Test/Programmers

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

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

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

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

  • File ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
    • File ํด๋ž˜์Šค์—” id(int), originalName(string), head(string), number(string), tail(string) ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
      • id ๊ฐ’์€ ๋ฌธ์ œ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” files ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ์ˆœ์„œ์ž…๋‹ˆ๋‹ค. head์™€ number๊ฐ€ ๊ฐ™์„ ๋•Œ, ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
      • ์ƒ์„ฑ์ž์—์„œ ์›๋ณธ ๋ฌธ์ž์—ด์„ head, number, tail๋กœ ์ž˜ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 
    • < ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ•ด์ค๋‹ˆ๋‹ค.
      • BST ์ž๋ฃŒ๊ตฌ์กฐ์ธ set์— File ๋ชฉ๋ก๋“ค์„ ์ €์žฅํ•ด ๋‘˜ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.
      • ์ปค์Šคํ…€ ํด๋ž˜์Šค๋ฅผ set์— ์ €์žฅํ•˜๋ ค๋ฉด < ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
        • ๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.
  • files ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ set<File>์— ๊ฐ์ฒด๋ฅผ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.
  • set<File>์„ ์ˆœํšŒํ•˜๋ฉด์„œ answer ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ ํ›„, ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <string>
#include <vector>
#include <set>
using namespace std;

class File {
public:
    File(int id, const string& fileName) {
        _id = id;
        _originalName = fileName;
        Init();
    }

    string ToLower(const string& str) {
        string result(str);
        for (int i = 0; i < result.length(); i++)
            result[i] = tolower(result[i]);
        return result;
    }

    bool operator<(const File& other) const {
        int dictionaryOrder = _head.compare(other._head);
        
        if (dictionaryOrder == 0) {
            if (_number.empty() or other._number.empty())
                return _id < other._id;

            int myNum = stoi(_number);
            int otherNum = stoi(other._number);
            return myNum != otherNum ? myNum < otherNum : _id < other._id;
        }
            
        return dictionaryOrder < 0;
    }

private:
    void Init() {
        int begin = 0, i = 0, end = _originalName.length();

        /* HEAD ์ฒ˜๋ฆฌ */
        for (; i < end; i++) {
            if (isdigit(_originalName[i])) {
                _head = ToLower(_originalName.substr(begin, i));
                begin = i;
                break;
            }
        }

        if (i == end) {
            _head = ToLower(_originalName.substr(begin, i));
            return;
        }

        /* NUMBER ์ฒ˜๋ฆฌ */
        for (; i < end; i++) {
            if (!isdigit(_originalName[i])) {
                _number = _originalName.substr(begin, i - begin);
                begin = i;
                break;
            }
        }

        if (i == end) {
            _number = _originalName.substr(begin, i - begin);
            return;
        }

        /* TAIL ์ฒ˜๋ฆฌ */
        _tail = _originalName.substr(begin);
    }

public:
    int _id;
    string _originalName;
    string _head;
    string _number;
    string _tail;
};

vector<string> solution(vector<string> files) {
    vector<string> answer;
    set<File> orderedFiles;

    for (int i = 0; i < files.size(); i++)
        orderedFiles.emplace(File(i, files[i]));

    for (const auto file : orderedFiles)
        answer.emplace_back(file._originalName);
    return answer;
}

 

 

 

728x90
๋ฐ˜์‘ํ˜•