From 5fd49587d0f6fe7e00e0f87569d00a37b2254bfb Mon Sep 17 00:00:00 2001 From: InSange Date: Sat, 10 Aug 2024 01:42:36 +0900 Subject: [PATCH 1/4] 2024-08-10 Minimum Height Trees --- InSange/BFS/310_Minimum Height Trees.cpp | 66 ++++++++++++++++++++++++ InSange/README.md | 1 + 2 files changed, 67 insertions(+) create mode 100644 InSange/BFS/310_Minimum Height Trees.cpp diff --git a/InSange/BFS/310_Minimum Height Trees.cpp b/InSange/BFS/310_Minimum Height Trees.cpp new file mode 100644 index 0000000..d482392 --- /dev/null +++ b/InSange/BFS/310_Minimum Height Trees.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; + +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) { + map> m; + vector ans; + vector check(n, false); + int* degree = new int[n] {0}; + + for (vector 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 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; + } +}; \ No newline at end of file diff --git a/InSange/README.md b/InSange/README.md index 537b1a9..13400de 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -26,6 +26,7 @@ | 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)] --- https://leetcode.com/problems/robot-collisions/ From 3fae6debbdff1f593cb8aa46746eef46f56d75f5 Mon Sep 17 00:00:00 2001 From: InSange Date: Sun, 11 Aug 2024 01:52:57 +0900 Subject: [PATCH 2/4] 2024-08-11 Magic Squares In Grid --- InSange/README.md | 1 + .../840_Magic Squares In Grid.cpp" | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 "InSange/\354\210\230\355\225\231/840_Magic Squares In Grid.cpp" diff --git a/InSange/README.md b/InSange/README.md index 13400de..9513d03 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -27,6 +27,7 @@ | 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)] --- https://leetcode.com/problems/robot-collisions/ diff --git "a/InSange/\354\210\230\355\225\231/840_Magic Squares In Grid.cpp" "b/InSange/\354\210\230\355\225\231/840_Magic Squares In Grid.cpp" new file mode 100644 index 0000000..f830c03 --- /dev/null +++ "b/InSange/\354\210\230\355\225\231/840_Magic Squares In Grid.cpp" @@ -0,0 +1,55 @@ +#include + +using namespace std; + +class Solution { +public: + bool Check(int r, int c, vector>& grid) + { + vector 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>& 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; + } +}; \ No newline at end of file From 8026614a089ac62f78e9893021cb66c98ed791bc Mon Sep 17 00:00:00 2001 From: InSange Date: Sun, 18 Aug 2024 01:45:59 +0900 Subject: [PATCH 3/4] 2024-08-18 Number of Senior Citiznes --- InSange/README.md | 1 + .../2678_Number of Senior Citizens.cpp" | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 "InSange/\353\254\270\354\236\220\354\227\264/2678_Number of Senior Citizens.cpp" diff --git a/InSange/README.md b/InSange/README.md index 9513d03..6e73fbf 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -28,6 +28,7 @@ | 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)] --- https://leetcode.com/problems/robot-collisions/ diff --git "a/InSange/\353\254\270\354\236\220\354\227\264/2678_Number of Senior Citizens.cpp" "b/InSange/\353\254\270\354\236\220\354\227\264/2678_Number of Senior Citizens.cpp" new file mode 100644 index 0000000..656e589 --- /dev/null +++ "b/InSange/\353\254\270\354\236\220\354\227\264/2678_Number of Senior Citizens.cpp" @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +class Solution { +public: + int countSeniors(vector& 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; + } +}; \ No newline at end of file From 9cff7431041821939065402022e2a775429582aa Mon Sep 17 00:00:00 2001 From: InSange Date: Wed, 21 Aug 2024 00:11:34 +0900 Subject: [PATCH 4/4] =?UTF-8?q?2024-08-20=20=EC=9B=94=EB=93=9C=EC=BB=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- InSange/README.md | 1 + .../6987.cpp" | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 "InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" diff --git a/InSange/README.md b/InSange/README.md index 6e73fbf..d690fec 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -29,6 +29,7 @@ | 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/ diff --git "a/InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" "b/InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" new file mode 100644 index 0000000..2c52416 --- /dev/null +++ "b/InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" @@ -0,0 +1,91 @@ +#include +#include + +using namespace std; + +vector> record; +int t; +bool draw_flag, flag; +vector> 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(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; +} \ No newline at end of file