From baf18abc79b3bce23792f244dc49cbc5b703bf6a Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:05:51 +0530 Subject: [PATCH] Create 947. Most Stones Removed with Same Row or Column --- ...ost Stones Removed with Same Row or Column | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 947. Most Stones Removed with Same Row or Column diff --git a/947. Most Stones Removed with Same Row or Column b/947. Most Stones Removed with Same Row or Column new file mode 100644 index 0000000..f06b8c6 --- /dev/null +++ b/947. Most Stones Removed with Same Row or Column @@ -0,0 +1,36 @@ +class Solution { +private: + int bfs(vector>& stones, int start_index, vector& visited, int n) { + queue q; + q.push(start_index); + visited[start_index] = true; + int count = 0; + while (!q.empty()) { + int index = q.front(); + q.pop(); + for (int i = 0; i < n; i++) { + if(!visited[i]){ + if((stones[i][0] == stones[index][0] || stones[i][1] == stones[index][1])){ + visited[i] = true; + q.push(i); + count++; + } + } + } + } + return count; + } + +public: + int removeStones(vector>& stones) { + int n = stones.size(); + vector visited(n, 0); + int ans = 0; + for (int i = 0; i < n; i++) { + if (!visited[i]) { + ans += bfs(stones, i, visited, n); + } + } + return ans; + } +};