-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from AlgoLeadMe/25-InSange
25-InSange
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |