aboutsummaryrefslogtreecommitdiff
path: root/other/number_provinces.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/number_provinces.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/number_provinces.cpp')
-rw-r--r--other/number_provinces.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/other/number_provinces.cpp b/other/number_provinces.cpp
new file mode 100644
index 0000000..2f733f9
--- /dev/null
+++ b/other/number_provinces.cpp
@@ -0,0 +1,41 @@
+// 547. Number of Provinces
+#include "leetcode.h"
+
+/*
+ * dthere are 'n' cities. some of them are connected, while some are not. if
+ * city 'a' is connected directly with city 'b', and city 'b' is connected
+ * directly with city 'c', then city 'a' is connected indirectly with city 'c'.
+ * a province is a group of directly or indirectly connected cities and no other
+ * cities outside of the group. given an 'n x n' matrix 'is_connected' where
+ * 'is_connected[i][j] = 1' if the i'th city and the j'th city are directly
+ * connected, and 'is_connected[i][j] = 0' otherwise. return the total number of
+ * provinces
+ */
+
+class Solution {
+ void dfs(int i, vvd & is_connected, vector<bool> &vis) {
+ vis[i] = true;
+ for (int j = 0; j < vis.size(); j++)
+ if (i != j && is_connected[i][j] && !vis[j])
+ dfs(j, is_connected, vis);
+ }
+
+public:
+ int findCircleNum(vvd & is_connected) {
+ if (is_connected.empty())
+ return 0;
+ int n = is_connected.size(), groups = 0;
+ vector<bool> vis(n, false);
+ for (int i = 0; i < vis.size(); i++)
+ groups += !vis[i] ? dfs(i, is_connected, vis), 1 : 0;
+ return groups;
+ }
+};
+
+int main() {
+ Solution obj;
+ vvd ic1 = {{1, 1, 0}, {1, 1, 0}, {0, 0, 1}};
+ vvd ic2 = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
+ printf("%d\n", obj.findCircleNum(ic1)); // expect: 2
+ printf("%d\n", obj.findCircleNum(ic2)); // expect: 3
+}