-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNumberProvinces.cpp
34 lines (31 loc) · 1007 Bytes
/
NumberProvinces.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Problem: https://leetcode.com/problems/number-of-provinces/
#include <queue>
#include <unordered_set>
#include <vector>
using namespace std;
class NumberProvinces {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
int num_nodes = isConnected.size();
unordered_set<int> seen;
int num_provinces = 0;
for (int i = 0; i < num_nodes; ++i) {
if (seen.find(i) != seen.end()) continue;
++num_provinces;
queue<int> nodes;
nodes.push(i);
while (not nodes.empty()) {
int curr_node = nodes.front(); nodes.pop();
seen.insert(curr_node);
for (int j = 0; j < num_nodes; ++j) {
if (isConnected[curr_node][j]) {
if (seen.find(j) == seen.end()) {
nodes.push(j);
}
}
}
}
}
return num_provinces;
}
};