Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

31-InSange #101

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

31-InSange #101

wants to merge 4 commits into from

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Nov 30, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Valid Arrangement of Pairs

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

3์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ฐœ์š”

๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ๋กœ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์„ 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง„๋‹ค.
Input: pairs = [[5,1],[4,5],[11,9],[9,4]]

์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋Š” ์ถœ๋ฐœ์ง€๊ณ  ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋กœ ์ฃผ์–ด์ง€๋Š” ๋…ธ๋“œ๋Š” ๋„์ฐฉ์ง€์ด๋‹ค.
5 -> 1
4 -> 5
11 -> 9
9 -> 4

์–ด๋–ค ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœ์„ ํ•˜์˜€์„ ๋•Œ ํ•ด๋‹น ๊ฐ„์„ ๋“ค์„ ๋ชจ๋‘ ์ด์šฉํ•˜์—ฌ ์œ ํšจํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.
์ฆ‰, ์ฃผ์–ด์ง„ pairs์˜ ๊ฐ„์„ ๋“ค์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•ด์„œ ๊ฐ ๋…ธ๋“œ๋“ค์„ ํ†ต๊ณผํ•˜๋Š” ์—ฐ์†๋œ ๋ฐฐ์—ด์„ ํ‘œํ˜„ํ•ด์•ผํ•จ.

์ค‘์š”ํ•œ ์ ์€ ์ฃผ์–ด์ง€๋Š” ๊ฐ„์„ ์€ ๋ฌด์กฐ๊ฑด ์œ ํšจํ•˜๋‹ค๋Š” ์ .
image
์ด๋ ‡๊ฒŒ ์ค‘๊ฐ„ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ„ ํ›„ ๋‹ค๋ฅธ ๊ฐ„์„ ๋“ค์„ ์ด์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†๋‹ค.
image
์œ„์˜ ์˜ˆ์ œ๋กœ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ชจ๋“  ๊ฐ„์„ ๋“ค์„ ํ™œ์šฉํ•ด์„œ ์œ ํšจํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

์ ‘๊ทผ ๋ฐฉ์‹

๋ฌด์กฐ๊ฑด ์œ ํšจํ•˜๋‹ค๋Š” ์ ์—์„œ ๋ชจ๋“  ๊ฐ„์„ ๋“ค์„ ๋‹ค ์ด์šฉํ•  ๋•Œ ๋‘ ๊ฐ€์ง€์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ์œ„์˜ ๊ทธ๋ฆผ ์ฒ˜๋Ÿผ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์ด ๋™์ผํ•˜๊ฒŒ ํ•˜๋‚˜์˜ ๊ฐ„์„ ์œผ๋กœ ์ด์–ด์ ธ ์žˆ๊ฑฐ๋‚˜ N๊ฐœ์˜ ๊ฐ„์„ ์œผ๋กœ ์ด์–ด์ ธ ์žˆ์„ ๊ฒฝ์šฐ.
  2. ์–ด๋–ค ํŠน์ •ํ•œ ๋…ธ๋“œ์—์„œ ์ง„์ถœ ์ฐจ์ˆ˜(OutDegree)๊ฐ€ ์ง„์ž… ์ฐจ์ˆ˜(InDegree)๋ณด๋‹ค 1 ๋งŽ์„ ๊ฒฝ์šฐ. ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœ์„ ํ•ด์ฃผ์–ด์•ผ ๋Œ๊ณ  ๋Œ์•„ N๋ฒˆ์˜ ์ง„์ž… ์ฐจ์ˆ˜ ๋งŒํผ ๋Œ์•˜์„ ๋•Œ ๋‚˜๋จธ์ง€ ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ ๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Constraints:

1 <= pairs.length <= 10^5
pairs[i].length == 2
0 <= starti, endi <= 10^9
starti != endi
No two pairs are exactly the same.
There exists a valid arrangement of pairs.

์ฃผ์–ด์ง„ ์ œ์•ฝ ์กฐ๊ฑด์—์„œ ์š”์†Œ์˜ ํฌ๊ธฐ๊ฐ€ 0์—์„œ 10^9์ด๊ธฐ์— ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด๋กœ ๊ฐ ๋…ธ๋“œ์— ์ง„์ถœ, ์ง„์ž… ์ฐจ์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
unordered_map์—์„œ ๊ฐ ๋…ธ๋“œ์˜ ์ง„์ถœ ๋…ธ๋“œ๋“ค์„ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๊ฒฐ์ •.
๊ทธ๋Ÿผ ๊ฐ ๋…ธ๋“œ๋“ค์—์„œ ์ฃผ์–ด์ง„ ๊ฐ„์„  ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ง„์ถœํ•  ๊ฒฝ์šฐ. ์ง„์ถœํ•  ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์„ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ visit ๋ฐฐ์—ด๋กœ ๊ด€๋ฆฌํ•ด์ฃผ๊ธฐ์—๋Š” ๋„ˆ๋ฌด ํฌ๊ธฐ์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋“ค์„ ์ง์ ‘ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ deque๋ฅผ ํ™œ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ถœ๋ฐœ์ง€๋ฅผ ์ •ํ•˜๊ธฐ์œ„ํ•ด ๊ฐ ์ง„์ž…, ์ง„์ถœ ์ฐจ์ˆ˜๋ฅผ ์ €์žฅํ•ด์ค„ unordred_map์„ ์ถ”๊ฐ€.

unordered_map<int, deque<int>> nodes; // n๊ฐœ์˜ ๋…ธ๋“œ๋“ค๊ณผ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์˜ ์ •๋ณด๋ฅผ ํฌํ•จ.
unordered_map<int, int> inDegree; // n๊ฐœ์˜ ๋…ธ๋“œ๋“ค๋กœ๋ถ€ํ„ฐ ์ง„์ž… ์ฐจ์ˆ˜
unordered_map<int, int> outDegree; // n๊ฐœ์˜ ๋…ธ๋“œ๋“ค๋กœ๋ถ€ํ„ฐ ์ง„์ถœ์ฐจ์ˆ˜

๋…ธ๋“œ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์œผ๋ฉด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
์šฐ๋ฆฌ๋Š” ๊ฐ„์„ ์„ ํ†ตํ•ด ์ˆœ์ฐจ์ ์œผ๋กœ ๋…ธ๋“œ๋“ค์„ ๋ฐฉ๋ฌธํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
DFS๋ฅผ ํ™œ์šฉํ•  ๊ฒƒ์ด๋ผ๋Š” ๊ณ„ํš๊นŒ์ง€๋Š” ์ข‹์•˜์ง€๋งŒ ํ’€์ด๊ฐ€ ์‰ฝ๊ฒŒ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ ๋‹ค๋ฅธ ๊ธ€์„ ์ฐธ๊ณ .

์ถœ๋ฐœ ๋…ธ๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ œ๊ฑฐ ํ•ด์ค€ ๋’ค ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
๊ทธ๋Ÿผ ๊ฒฐ๊ตญ ๋น„์›Œ์ ธ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๊ณ .
ํ•ด๋‹น ๋น„์›Œ์ ธ์žˆ๋Š” ๋…ธ๋“œ๋Š” ์ตœ์ข… ๋…ธ๋“œ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. (๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์ด๊ธฐ์—)
๊ทธ๋ ‡๊ฒŒ ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๋Œ์•„์˜ค๋‹ค๋ณด๋ฉด ๊ฐ๊ฐ์˜ ๋…ธ๋“œ๋“ค์ด ๋น„์›Œ์ ธ์žˆ์„ ๊ฒƒ์ด๊ณ  ๋น„์›Œ์ ธ์žˆ๋Š” ๋…ธ๋“œ๋“ค์„ result ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๋ถ€ํ„ฐ ์ถ”๊ฐ€๋ฅผ ํ•˜์˜€๊ธฐ์— result ๋ฐฐ์—ด์— ๋งˆ์ง€๋ง‰์œผ๋กœ push๋˜๋Š” ๋…ธ๋“œ๋Š” ์ถœ๋ฐœ์ง€์ ์ด ๋  ๊ฒƒ์ด๋‹ค.

๊ฐ ๋…ธ๋“œ๋ฅผ ๋‘ ๊ฐœ์”ฉ ๋ฌถ์–ด ๊ฑฐ๊พธ๋กœ ans 2์ฐจ์› ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ๋ฉด ๋.

์ค‘๊ฐ„์—
pairs = [[1,2],[1,3],[2,1]]
์œ„์™€ ๊ฐ™์ด ์ฃผ์–ด์กŒ์„ ๋•Œ
1->3์„ ๋จผ์ € ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฑฐ์ง€ ์˜๋ฌธ์ด ๋“ค์—ˆ์—ˆ๋Š”๋ฐ
3์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๋“  2๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๋“  ๊ฐ„์— nodes[3]์˜ ๊ฐ’์€ ๋น„์›Œ์ ธ์žˆ๊ธฐ์— ๋ฌด์กฐ๊ฑด result์— ์ฒซ๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋  ๊ฒƒ ์ด๋‹ค.
2์˜ ๊ฒฝ์šฐ ๋‹ค์‹œ 1 ๋…ธ๋“œ๋กœ ๋Œ์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
result = [3, 1, 2, 1] ๋กœ ๋  ๊ฒƒ์ด๋‹ค.

์†Œ์Šค ์ฝ”๋“œ

class Solution {
public:
    unordered_map<int, deque<int>> nodes;
    unordered_map<int, int> inDegree;
    unordered_map<int, int> outDegree;
    
    vector<int> result;
    
    void DFS(int index)
    {
        while(!nodes[index].empty())
        {
            int nextNode = nodes[index].front();
            nodes[index].pop_front();
            DFS(nextNode);
        }
        result.push_back(index); // ๊ฒฐ๊ตญ. ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด. ์ฆ‰ ๋น„์›Œ์ ธ์žˆ๋Š” ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ๋„์ฐฉ์ง€ ๊ฒฝ๋กœ๊ฐ€ ๊ฑฐ๊พธ๋กœ ์ด์–ด์ง€๊ฒŒ ๋จ.
    }
    
    vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {
        for(const vector<int>& pair : pairs)
        {
            int depart = pair[0];
            int dest = pair[1];
            
            nodes[depart].push_back(dest);
            inDegree[dest]++;
            outDegree[depart]++;
        }
        
        int startNode = -1;
        
        for(const auto& degree : outDegree)
        {
            int curNode = degree.first;
            if(outDegree[curNode] == inDegree[curNode] + 1) // ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœ์ง€๊ฐ€ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ๋“ค์–ด์˜ค๋Š” ์ง„์ž…์˜ ๊ฐœ์ˆ˜ + 1์ผ ๊ฒฝ์šฐ. ์ด ๋…ธ๋“œ๋Š” ๋Œ๊ณ  ๋Œ์•„ ๋‹ค์‹œ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ์•ผ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ์Œ.
            {
                startNode = curNode;
                break;
            }
        }
        
        if(startNode == -1) // ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ๋ฐœ๊ฒฌ ๋ชปํ–ˆ๋‹ค๋ฉด ๋ชจ๋“  ๋…ธ๋“œ๋Š” ๊ฐ๊ฐ ํ•˜๋‚˜์˜ ์ง„์ž…, ์ง„์ถœ์ง€๋ฅผ ๋™์ผํ•˜๊ฒŒ ๊ฐ€์ง€๋Š” ๊ฒƒ
        {
            startNode = pairs[0][0];
        }
        
        DFS(startNode);
        
        vector<vector<int>> ans;
        for(int i = result.size() - 1; i > 0; i--)
        {
            ans.push_back({result[i], result[i-1]}); // ์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€๋ฅผ ๊ฑฐ๊พธ๋กœ ๋ณ€๊ฒฝ
        }
        
        return ans;
    }
};

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ•ญ์ƒ ์˜ˆ์™ธ์‚ฌํ•ญ์„ ์ƒ๊ฐํ•˜๊ณ  ํ’€๋‹ค๋ณด๋‹ˆ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์Œ์—๋„ ์กฐ๊ธˆ ์˜ค๋ž˜๊ฑธ๋ ธ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant