Commit 27539330 authored by yaozc's avatar yaozc
Browse files

keep going

parent a9ecb3f5
......@@ -721,24 +721,42 @@ public:
// 本质为directed graph的寻找cycle问题
//在这个问题中,使用DFS 应用到 Kahn’s algorithm for Topological Sorting可以寻找graph是否有cycle
// DFS问题不一定要使用stack,很多时候用recursion来解决DFS
// 此题的逻辑为:一个vector (visited),一个stack(visiting),若一个新的点在这两个容器中出现过(先前被访问过),那么此graph有cycle
// 此题的逻辑为:一个vector (visited),一个stack(visiting),若一个新的点在visiting中出现过(先前被访问过),那么此graph有cycle
//因为stack不利于查找,所以答案中用了set替代stack来做visiting。将DFS写成rucursion即可
// https://www.youtube.com/watch?v=VvKwqfXri0I&t=558s
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
cout<<"here"<<endl;
vector<vector<int>> graph = graph_maker(numCourses, prerequisites);
cout<<"here"<<endl;
vector<vector<int>> graph(prerequisites.size());
for(int i=0; i<numCourses; i++){
vector<int> temp;
graph.push_back(temp);
}
graph_maker(prerequisites, graph);
cout<<"here"<<endl;
stack<int> visiting;
unordered_set<int> set_visiting;
unordered_set<int> visited;
visiting.push(0);
set_visiting.insert(0);
// DFS做法:每访问一个点就把这个点放进stack中,若这个点是此阶段终点(无其他点可以从这个点开始访问,那么就从stack中pop掉并mark为visited!)
for(int i=0; i<numCourses; i++){
// if this node not in visiting
if(visiting.find(i) == visiting.end()){
visiting.push(i);
for(int inner=0; inner<graph[i].size(); inner++){
if(visiting.find(graph[i][inner]) != visiting.end())return false;
visiting.push(i);
}
}
}
while(!set_visiting.empty()){
int current = visiting.top();
cout<<current<<endl;
cout<<current<<endl;
visiting.pop();
set_visiting.erase(current);
visited.insert(current);
......@@ -753,18 +771,183 @@ cout<<"here"<<endl;
return true;
}
vector<vector<int>> graph_maker(vector<vector<int>>& prerequisites){
vector<vector<int>> graph(prerequisites.size());
for(auto v: prerequisites){
graph[v[0]].push_back(v[1]]);
void graph_maker(vector<vector<int>>& prerequisites, vector<vector<int>>& graph){
cout<<"1"<<endl;
cout<<"2"<<endl;
for(int i=0; i<prerequisites.size(); i++){
graph[prerequisites[i][0]].push_back(prerequisites[i][1]);
}
return graph;
}
};
Lowest Common Ancestor of a Binary Tree
// 因为这不是一颗binary search tree,所以相当于每次只能看一个点并且要查看完所有点直到p和q都找到为止
// 写recursion时候很重要一点!!!一定要想好policy
//参考这个小哥视频: https://www.youtube.com/watch?v=py3R23aAPCA
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return nullptr;
if(root->val == p->val) return root;
if(root->val == q->val) return root;
TreeNode* leftSub = lowestCommonAncestor(root->left, p, q);
TreeNode* rightSub = lowestCommonAncestor(root->right, p, q);
if(leftSub && rightSub) return root;
else if(leftSub && !rightSub) return leftSub;
else if(!leftSub && rightSub) return rightSub;
return nullptr;
}
};
Diameter of Binary Tree
// 讲道理这题吃的不是很透。。。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int res = 0;
int diameterOfBinaryTree(TreeNode* root) {
max_depth(root);
return this->res;
}
int max_depth(TreeNode* root){
if (!root) return 0;
int left = max_depth(root->left);
int right = max_depth(root->right);
this->res = max(res, left + right);
return max(left, right) + 1;
}
};
class Solution {
public:
int cutOffTree(vector<vector<int>>& forest) {
pair<int, int> current_pos;
current_pos = make_pair(0, 0);
pair<int, int> dest;
int steps = 0;
while(!clean_ground(forest)){
dest = lowest_tree_pos(forest);
if (dest.first == -1) return false;
int temp = setps_to_pos(current_pos, dest, forest);
if(temp==-1) return -1;
steps += temp;
current_pos = dest;
}
return steps;
}
bool clean_ground(vector<vector<int>>& forest){
for(int outer=0; outer<forest.size(); outer++){
for(int inner=0; inner<forest[i].size(); inner++){
if (forest[outer][inner] > 1) return false;
}
}
return true;
}
pair<int, int> lowest_tree_pos(vector<vector<int>>& forest){
int lowest = MAX_INT;
pair<int, int> dest = makr_pair(-1, -1);
for(int outer=0; outer<forest.size(); outer++){
for(int inner=0; inner<forest[i].size(); inner++){
if((forest[outer][inner]>1) && (forest[outer][inner] < lowest)) dest = make_pair(outer, inner);
}
}
// return -1, -1 if no trees need to cut
if (dest.first==-1) return dest;
forest[dest.first][dest.second] = 1;
return dest;
}
int setps_to_pos(pair<int, int>& current_pos, pair<int, int> dest, vector<vector<int>>& forest){
int total_steps = 0;
queue<pair<int, int>> q;
q.push(current_pos);
vector<vector<bool>> visited;
for(int a=0; a<forest.size(); a++){
visited.push_back(vector<bool>(forest[0].size(), false));
}
while(!q.empty()){
pair<int, int> curr = q.front();
q.pop();
visited[curr.first][curr.second] = true;
if(curr.first!=dest.first || curr.second!=dest.second) total_steps++;
else {return total_steps;}
//left one
if(curr.first-1>0 && forest[curr.first-1][curr.second]>1 && !visited[curr.first-1][curr.second])
q.push(makr_pair(curr.first-1, curr.second));
//up one
if(curr.second-1>0 && forest[curr.first][curr.second-1]>1 && !visited[curr.first][curr.second-1])
q.push(makr_pair(curr.first, curr.second-1));
//right one
if(curr.first+1<forest[0].size() && forest[curr.first+1][curr.second]>1 && !visited[curr.first+1][curr.second])
q.push(makr_pair(curr.first+1, curr.second));
//down one
if(curr.second+1<forest.size() && forest[curr.first][curr.second+1]>1 && !visited[curr.first][curr.second+1])
q.push(makr_pair(curr.first, curr.second+1));
}
return -1;
}
};
......@@ -785,3 +968,102 @@ cout<<"here"<<endl;
//
/*#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <unordered_set>
using namespace std;
bool clean_ground(vector<vector<int>>& forest){
for(int outer=0; outer<forest.size(); outer++){
for(int inner=0; inner<forest[outer].size(); inner++){
if (forest[outer][inner] > 1) return false;
}
}
return true;
}
pair<int, int> lowest_tree_pos(vector<vector<int>>& forest){
int lowest = INT_MAX;
pair<int, int> dest = make_pair(-1, -1);
for(int outer=0; outer<forest.size(); outer++){
for(int inner=0; inner<forest[outer].size(); inner++){
if((forest[outer][inner]>1) && (forest[outer][inner] < lowest)) {lowest=forest[outer][inner]; dest = make_pair(outer, inner);}
}
}
// return -1, -1 if no trees need to cut
if (dest.first==-1) return dest;
return dest;
}
int setps_to_pos(pair<int, int>& current_pos, pair<int, int> dest, vector<vector<int>>& forest){
int total_steps = 0;
queue<pair<int, int>> q;
q.push(current_pos);
vector<vector<bool>> visited;
for(int a=0; a<forest.size(); a++){
visited.push_back(vector<bool>(forest[0].size(), false));
}
while(!q.empty()){
pair<int, int> curr = q.front();
q.pop();
visited[curr.first][curr.second] = true;
if(curr.first!=dest.first || curr.second!=dest.second) total_steps++;
else {return total_steps;}
//up one
if(curr.first-1>=0 && forest[curr.first-1][curr.second]>1 && !visited[curr.first-1][curr.second])
q.push(make_pair(curr.first-1, curr.second));
//left one
if(curr.second-1>=0 && forest[curr.first][curr.second-1]>1 && !visited[curr.first][curr.second-1])
q.push(make_pair(curr.first, curr.second-1));
//down one
if(curr.first+1<forest.size() && forest[curr.first+1][curr.second]>1 && !visited[curr.first+1][curr.second])
q.push(make_pair(curr.first+1, curr.second));
//right one
if(curr.second+1<forest[0].size() && forest[curr.first][curr.second+1]>1 && !visited[curr.first][curr.second+1])
q.push(make_pair(curr.first, curr.second+1));
}
return -1;
}
int cutOffTree(vector<vector<int>>& forest) {
pair<int, int> current_pos;
current_pos = make_pair(0, 0);
pair<int, int> dest;
int steps = 0;
while(!clean_ground(forest)){
dest = lowest_tree_pos(forest);
if (dest.first == -1) return -1;
int temp = setps_to_pos(current_pos, dest, forest);
if(temp==-1) return -1;
steps += temp;
current_pos = dest;
forest[dest.first][dest.second] = 1;
}
return steps;
}
int main()
{
vector<vector<int>> test {{54581641,64080174,24346381,69107959},{86374198,61363882,68783324,79706116},{668150,92178815,89819108,94701471},{83920491,22724204,46281641,47531096},{89078499,18904913,25462145,60813308}};
cout<<cutOffTree(test);
return 0;
} */
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment