Skip to content

Commit

Permalink
2024-12-01 Valid Arrangement of Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed Nov 30, 2024
1 parent ea88f3d commit 9159fe2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions InSange/DFS/2097 Valid Arrangement of Pairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <unordered_map>
#include <deque>
#include <vector>

using namespace std;

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;
}
};
1 change: 1 addition & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
| 28์ฐจ์‹œ | 2024.08.21 | ๋ฐฑํŠธ๋ž˜ํ‚น | [์›”๋“œ์ปต](https://www.acmicpc.net/problem/6987) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/94)]
| 29์ฐจ์‹œ | 2024.08.25 | ๋ฌธ์ž์—ด | [Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/98)]
| 30์ฐจ์‹œ | 2024.09.06 | ๋ฌธ์ž์—ด | [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) | [#30](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/100)]
| 31์ฐจ์‹œ | 2024.12.01 | DFS | [Valid Arrangement of Pairs](https://leetcode.com/problems/valid-arrangement-of-pairs/) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/101)]
---

https://leetcode.com/problems/robot-collisions/
Expand Down

0 comments on commit 9159fe2

Please sign in to comment.