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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};
5 changes: 5 additions & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
| 25์ฐจ์‹œ | 2024.08.10 | BFS | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/87)]
| 26์ฐจ์‹œ | 2024.08.11 | ์ˆ˜ํ•™ | [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/89)]
| 27์ฐจ์‹œ | 2024.08.17 | ๋ฌธ์ž์—ด | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [#27](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/91)]
| 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/
Find the Closest Palindrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <string>

using namespace std;

class Solution {
public:
long long Convert(long long& num)
{
string s = to_string(num);
int n = s.length();
int l = (n - 1) / 2;
int r = n / 2;
while (l >= 0) s[r++] = s[l--];
return stoll(s);
}

long long UpPal(long long num)
{
long long left = 0;
long long right = num;
long long ans = INT_MIN;

while (left <= right)
{
long long mid = (right + left) / 2;
long long palin = Convert(mid);
if (palin < num)
{
ans = palin;
left = mid + 1;
}
else
{
right = mid - 1;
}
}

return ans;
}

long long DownPal(long long num)
{
long long left = num;
long long right = 1e18;
long long ans = INT_MIN;

while (left <= right) {
long long mid = (right + left) / 2;
long long palin = Convert(mid);
if (palin > num)
{
ans = palin;
right = mid - 1;
}
else
{
left = mid + 1;
}
}

return ans;
}

string nearestPalindromic(string n) {
long long num = stoll(n);
long long a = UpPal(num);
long long b = DownPal(num);

if (abs(a - num) <= abs(b - num)) return to_string(a);

return to_string(b);
}
};
91 changes: 91 additions & 0 deletions InSange/๋ฐฑํŠธ๋ž˜ํ‚น/6987.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> record;
int t;
bool draw_flag, flag;
vector<pair<int, int>> game = { {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5},
{1, 2}, {1, 3}, {1, 4}, {1, 5},
{2, 3}, {2, 4}, {2, 5},
{3, 4}, {3, 5},
{4, 5} };

bool CheckPlay(int round)
{
if (round == 15)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 3; j++)
{
if (record[i][j]) return false;
}
}
return true;
}

int firstTeam, secondTeam;
firstTeam = game[round].first;
secondTeam = game[round].second;

if (record[firstTeam][0] && record[secondTeam][2]) // first team win, second team lose
{
--record[firstTeam][0];
--record[secondTeam][2];
if (CheckPlay(round + 1)) return true;
++record[firstTeam][0];
++record[secondTeam][2];
}

if (record[firstTeam][1] && record[secondTeam][1]) // first team draw, second team draw
{
--record[firstTeam][1];
--record[secondTeam][1];
if (CheckPlay(round + 1)) return true;
++record[firstTeam][1];
++record[secondTeam][1];
}

if (record[firstTeam][2] && record[secondTeam][0]) // first team lose, second team win
{
--record[firstTeam][2];
--record[secondTeam][0];
if (CheckPlay(round + 1)) return true;
++record[firstTeam][2];
++record[secondTeam][0];
}

return false;
}

void Solve()
{
record.assign(6, vector<int>(3, 0));
t = 4;

while (t--)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> record[i][j];
}
}

if (CheckPlay(0)) cout << 1 << " ";
else cout << 0 << " ";
}
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);

Solve();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <vector>
#include <unordered_map>

using namespace std;


struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
ListNode* modifiedList(vector<int>& nums, ListNode* head) {
ListNode* removeList = new ListNode();
ListNode* removeHead;
unordered_map<int, bool> um;

for (int& val : nums) um[val] = true;

removeList->next = head;
removeHead = removeList;

while (removeHead->next)
{
if (um[removeHead->next->val] == true) removeHead->next = removeHead->next->next;
else removeHead = removeHead->next;
}

return removeList->next;
}
};