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

32-InSange #102

Open
wants to merge 5 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
57 changes: 57 additions & 0 deletions InSange/BFS/2577. Minimum Time to Visit a Cell In a Grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <vector>
#include <queue>

using namespace std;

class Solution {
public:
int minimumTime(vector<vector<int>>& grid) {
if (grid[0][1] > 1 && grid[1][0] > 1) // °¥ ¼ö ÀÖ´Â ±æÀÌ Ã³À½ºÎÅÍ ¾øÀ¸¸é
{
return -1;
}

int row = grid.size();
int col = grid[0].size();

vector<vector<int>> val;
val.assign(row, vector<int>(col, INT_MAX));

priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;
int dy[4] = { 0, 0, -1, 1 };
int dx[4] = { 1, -1, 0, 0 };

pq.push({ 0, 0, 0 });
val[0][0] = 0;

while (!pq.empty())
{
int curY = pq.top()[1];
int curX = pq.top()[2];
int curTime = pq.top()[0];
pq.pop();

if (curY == row - 1 && curX == col - 1)
{
return curTime;
}

for (int i = 0; i < 4; i++)
{
int nextY = curY + dy[i];
int nextX = curX + dx[i];

if (nextY < 0 || nextY >= row || nextX < 0 || nextX >= col) continue; // Out of Bounds
int nextTime = max(curTime + 1, grid[nextY][nextX] + ((grid[nextY][nextX] - curTime) % 2 == 0 ? 1 : 0));

if (val[nextY][nextX] > nextTime)
{
pq.push({ nextTime, nextY, nextX });
val[nextY][nextX] = nextTime;
}
}
}

return -1;
}
};
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;
}
};
7 changes: 5 additions & 2 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
| 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)]
| 32차시 | 2024.12.03 | BFS | [Minimum Time to Visit a Cell In a Grid](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/102)]
---

https://leetcode.com/problems/robot-collisions/
74 changes: 74 additions & 0 deletions InSange/문자열/564_Find the Closest Palindrome.cpp
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;
}
};
Loading