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

28-InSange #94

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
66 changes: 66 additions & 0 deletions InSange/BFS/310_Minimum Height Trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <vector>
#include <map>
#include <queue>

using namespace std;

class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
map<int, vector<int>> m;
vector<int> ans;
vector<bool> check(n, false);
int* degree = new int[n] {0};

for (vector<int> node : edges)
{
m[node[0]].push_back(node[1]);
m[node[1]].push_back(node[0]);
degree[node[0]]++;
degree[node[1]]++;
}
int current_n = n;

queue<int> q;
if (current_n > 2)
{
for (int i = 0; i < n; i++)
{
if (degree[i] == 1 && check[i] == false)
{
check[i] = true;
q.push(i);
current_n--;
}
}
}
while (current_n > 2 && !q.empty())
{
int size = q.size();

for (int j = 0; j < size; j++)
{
int remove_n = q.front();
q.pop();

for (auto node : m[remove_n])
{
degree[node]--;
if (degree[node] == 1)
{
q.push(node);
check[node] = true;
current_n--;
}
}
}
}

for (int i = 0; i < n; i++)
{
if (check[i] == false) ans.push_back(i);
}

return ans;
}
};
4 changes: 4 additions & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
| 22μ°¨μ‹œ | 2024.07.31 | DP | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/81)]
| 23μ°¨μ‹œ | 2024.08.03 | μŠ¬λΌμ΄λ”© μœˆλ„μš° | [Minimum Swaps to Group All 1's Together 2](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [#23](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/82)]
| 24μ°¨μ‹œ | 2024.08.04 | BFS | [트리](https://www.acmicpc.net/problem/1068) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/83)]
| 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)]
---

https://leetcode.com/problems/robot-collisions/
24 changes: 24 additions & 0 deletions InSange/λ¬Έμžμ—΄/2678_Number of Senior Citizens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
int countSeniors(vector<string>& details) {
int seniorCount = 0;

for (string& passengerInfo : details) {
int ageTens = passengerInfo[11] - '0';
int ageOnes = passengerInfo[12] - '0';

int age = ageTens * 10 + ageOnes;

if (age > 60) {
seniorCount++;
}
}

return seniorCount;
}
};
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;
}
55 changes: 55 additions & 0 deletions InSange/μˆ˜ν•™/840_Magic Squares In Grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <vector>

using namespace std;

class Solution {
public:
bool Check(int r, int c, vector<vector<int>>& grid)
{
vector<bool> isCheck(10, false);

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int num = grid[r + i][c + j];
if (num < 1 || num > 9) return false;
if (isCheck[num]) return false;
isCheck[num] = true;
}
}

int standard_num = grid[r][c] + grid[r + 1][c + 1] + grid[r + 2][c + 2];
if (standard_num != grid[r][c + 2] + grid[r + 1][c + 1] + grid[r + 2][c]) return false;

for (int i = 0; i < 3; i++)
{
if (standard_num != (grid[r + i][c] + grid[r + i][c + 1] + grid[r + i][c + 2])) return false;
}

for (int i = 0; i < 3; i++)
{
if (standard_num != (grid[r][c + i] + grid[r + 1][c + i] + grid[r + 2][c + i])) return false;
}

return true;
}

int numMagicSquaresInside(vector<vector<int>>& grid) {
int col = grid[0].size(), row = grid.size();

if (col < 3 || row < 3) return 0;

int ans = 0;

for (int i = 0; i < row - 2; i++)
{
for (int j = 0; j < col - 2; j++)
{
if (Check(i, j, grid)) ans++;
}
}

return ans;
}
};