Skip to content

Commit

Permalink
Merge pull request 7oSkaaa#1103 from aboelsooud/main
Browse files Browse the repository at this point in the history
add the 18'th day problem solution
  • Loading branch information
7oSkaaa committed May 18, 2023
2 parents f4b4dc5 + 66699ad commit 4cdf7d1
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Author: Mahmoud Aboelsoud

class Solution {
public:
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {
// to find the minimum number of vertices to reach all nodes we need to find the nodes with zero in degree
// because we can reach all nodes from the nodes with zero in degree
// we can do that by counting the in degree of each node
// then loop over the nodes and add the nodes with zero in degree to the answer

// in_degree: in_degree[i] -> the in degree of node i
// ans: the answer
vector<int> in_degree(n), ans;

// count the in degree of each node
for(auto&i: edges) in_degree[i[1]]++;

// loop over the nodes and add the nodes with zero in degree to the answer
for(int i = 0; i < n; i++){
if(!in_degree[i]) ans.emplace_back(i);
}

// return the answer
return ans;
}
};

0 comments on commit 4cdf7d1

Please sign in to comment.