aboutsummaryrefslogtreecommitdiff
path: root/most_stones_remove.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /most_stones_remove.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'most_stones_remove.cpp')
-rw-r--r--most_stones_remove.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/most_stones_remove.cpp b/most_stones_remove.cpp
new file mode 100644
index 0000000..5283383
--- /dev/null
+++ b/most_stones_remove.cpp
@@ -0,0 +1,35 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int removeStones(vvd(int) & stones) {
+ int n = stones.size(), c = 0;
+ unordered_map<int, vector<int>> rows, cols;
+ for (int i = 0; i < n; ++i) {
+ rows[stones[i][0]].push_back(i);
+ cols[stones[i][1]].push_back(i);
+ }
+ unordered_set<int> seen;
+ function<int(int)> dfs;
+ dfs = [&](int i) -> int {
+ if (seen.count(i))
+ return 0;
+ seen.insert(i);
+ int j = stones[i][0], k = stones[i][1];
+ for (auto r : rows[j])
+ dfs(r);
+ for (auto c : cols[k])
+ dfs(c);
+ return 1;
+ };
+ for (int i = 0; i < n; ++i)
+ c += dfs(i);
+ return n - c;
+ }
+};
+
+int main() {
+ Solution obj;
+ vvd(int) stones = {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}};
+ cout << obj.removeStones(stones);
+}