[Programmers] Lv2. ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ | C++

2023. 6. 28. 19:55ใ†Coding Test/Programmers

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

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

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

programmers.co.kr

 

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

 

ํ–‰๋ ฌ ํฌ๊ธฐ๊ฐ€ ์ตœ๋Œ€ \( 10000 \times 10000 \)์ด๋ผ์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€ ์•Š์„๊นŒ ๊ฑฑ์ •ํ–ˆ๋Š”๋ฐ, ๋‹คํ–‰ํžˆ ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š๋„ค์š”. ๊ทธ๋ƒฅ ๋ฌธ์ œ์—์„œ ๋งํ•˜๋Š”๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทผ๋ฐ, ์ต์ˆ™์น˜๊ฐ€ ์•Š์•„์„œ ์‹œ๊ฐ„์ด ์ข€ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋„ค์š”.

 

  • rows \( \times \) columns ํฌ๊ธฐ์˜ 2์ฐจ์› ๋ฐฐ์—ด ๋งŒ๋“ค๊ณ , ๊ฐ’ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ
  • ์ฃผ์–ด์ง„ query์— ๋Œ€ํ•ด ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ
    • ์ €๋Š” ๊ฐ’์„ ์Šค์™‘ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „์ด๋‹ˆ, ์œ„ \( \rightarrow \) ์˜ค๋ฅธ์ชฝ \( \rightarrow \) ์•„๋ž˜ \( \rightarrow \) ์™ผ์ชฝ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ํšŒ์ „ํ•˜๋ฉด์„œ ๋งˆ์ฃผ์นœ ๊ฐ’๋“ค์€ set ์ž๋ฃŒ๊ตฌ์กฐ์— ๋ณ„๋„๋กœ ์ €์žฅํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
  • ํšŒ์ „์„ ๋งˆ์นœ ํ›„, set ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๋‹ต์ž…๋‹ˆ๋‹ค.

 

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

#include <vector>
#include <set>
using namespace std;
vector<vector<int>> GMatrix;

class RotatedArea {
public:
    RotatedArea(int x1, int y1, int x2, int y2) : _x1(x1), _y1(y1), _x2(x2), _y2(y2) 
    {
        int row = --x1, col = --y1;
        _buffer = GMatrix[row][col];

        // 1. ์œ—๋ณ€ ์›์†Œ๋“ค ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
        for (++col; col < y2; ++col)
            Swap(row, col);
        
        // 2. ์˜ค๋ฅธ๋ณ€ ์›์†Œ๋“ค ์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™
        for (++row, --col; row < x2; ++row)
            Swap(row, col);

        // 3. ์•„๋žซ๋ณ€ ์›์†Œ๋“ค ์™ผ์ชฝ์œผ๋กœ ์ด๋™
        for (--row, --col; col >= y1; --col)
            Swap(row, col);

        // 4. ์™ผ๋ณ€ ์›์†Œ๋“ค ์œ—์ชฝ์œผ๋กœ ์ด๋™
        for (--row, ++col; row >= x1; --row)
            Swap(row, col);
    }

    int GetMinValue() { return *_elements.begin(); }
private:
    void Swap(int row, int col)
    {
        int currentValue = GMatrix[row][col];
        _elements.emplace(_buffer);
        GMatrix[row][col] = _buffer;
        _buffer = currentValue;
    }

public:
    int _buffer = 0;    // ํšŒ์ „์‹œํ‚ฌ ๋•Œ ์ด์ „ ๊ฐ’์„ ์ €์žฅํ•ด๋‘๋Š” ์ž„์‹œ ๊ณต๊ฐ„
    int _x1, _y1;
    int _x2 ,_y2;
private:
    set<int> _elements;
};


vector<int> solution(int rows, int columns, vector<vector<int>> queries) {
    vector<int> answer;
    
    // ํ–‰๋ ฌ ์ดˆ๊ธฐํ™”
    for (int row = 0; row < rows; row++) {
        vector<int> currentRow(columns);

        for (int col = 0; col < columns; col++)
            currentRow[col] = row * columns + col + 1;
        GMatrix.emplace_back(currentRow);
    }

    
    for (const auto& query : queries) {
        int x1 = query[0], y1 = query[1];
        int x2 = query[2], y2 = query[3];
    
        RotatedArea area(x1, y1, x2, y2);
        answer.emplace_back(area.GetMinValue());
    }

    return answer;
}

 

 

 

728x90
๋ฐ˜์‘ํ˜•