947. Most Stones Removed | BFS | DFS | Leetcode POTD Explained

  Рет қаралды 169

Jeevesh Rai

Jeevesh Rai

Күн бұрын

Пікірлер: 4
@codeby_naruto
@codeby_naruto 21 күн бұрын
Code :- class Solution { public: // Function to check if two stones are in the same group (same row or column) bool areSameGroup(vector& stone1, vector& stone2) { return stone1[0] == stone2[0] || stone1[1] == stone2[1]; } // Depth-first search to mark all stones connected to the current stone void dfs(vector& visited, vector& stones, int idx) { visited[idx] = 1; for (int i = 0; i < stones.size(); i++) { if (!visited[i] && areSameGroup(stones[idx], stones[i])) { dfs(visited, stones, i); } } } // Main function to find the maximum number of stones that can be removed int removeStones(vector& stones) { int numStones = stones.size(); vector visited(numStones, 0); int groups = 0; // Iterate over each stone and perform DFS if it's not visited for (int i = 0; i < numStones; i++) { if (!visited[i]) { groups++; dfs(visited, stones, i); } } // The maximum number of stones that can be removed is total stones minus the number of groups return numStones - groups; } };
@shubhamjaiswal7645
@shubhamjaiswal7645 21 күн бұрын
@MP-ny3ep
@MP-ny3ep 21 күн бұрын
Beautiful explanation bro
@codeby_naruto
@codeby_naruto 21 күн бұрын
Thank you ❤️❤️
Lecture 7: LeetCode Problem Solving Session
42:43
CodeHelp - by Babbar
Рет қаралды 824 М.
Nastya and balloon challenge
00:23
Nastya
Рет қаралды 54 МЛН
Minecraft Creeper Family is back! #minecraft #funny #memes
00:26
ПРИКОЛЫ НАД БРАТОМ #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
Postgres just got even faster
26:42
Hussein Nasser
Рет қаралды 23 М.
Codeforces Round 971 (Div 4) | Video Solutions - A to G1 | by Gaurish Baliga | TLE Eliminators
1:46:24
874. Walking Robot Simulation | Leetcode POTD Explained
18:37
Nastya and balloon challenge
00:23
Nastya
Рет қаралды 54 МЛН