Count Sub Islands | Easiest Thought Process | Leetcode 1905 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 104th Video of our Playlist "Array 1D/2D : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a simple 2D Array Problem : Count Sub Islands | Easiest Thought Process | Leetcode 1905 | codestorywithMIK
We will solve it using BFS and DFS. Your homework is to solve it using DSU also.
If you want to revise DSU - • Disjoint Set Union | DSU | Graph Conc...
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Count Sub Islands | Easiest Thought Process | Leetcode 1905 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) - https://github.com/MAZHARMIK/Intervie...
Leetcode Link : https://leetcode.com/problems/count-s...
My DP Concepts Playlist : • Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : • Graph Concepts & Qns - 1 : Graph will...
My Recursion Concepts Playlist : • Introduction | Recursion Concepts And...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1 (DFS)
Time Complexity (T.C.): O(m×n)
Space Complexity (S.C.): O(1) (excluding the recursion stack)
This approach uses Depth-First Search (DFS) to explore potential sub-islands in grid2. The idea is to traverse each land cell (grid2[i][j] == 1) and check if it corresponds to land in grid1. If it does, the DFS continues exploring all connected cells in four directions (down, up, right, left). Cells that are visited are marked to avoid reprocessing. If all the connected cells in grid1 and grid2 align (both contain land), then the island in grid2 is considered a sub-island. The algorithm iterates through all cells in the grid, triggering DFS when it encounters unvisited land. The space complexity does not account for the recursion stack, which can grow to a depth of O(m×n) in the worst case.
Approach 2 (BFS)
Time Complexity (T.C.): O(m×n)
Space Complexity (S.C.): O(m×n) (due to the queue)
This approach uses Breadth-First Search (BFS) to solve the same problem of identifying sub-islands. Instead of recursively exploring connected land, BFS uses a queue to iteratively visit all neighboring land cells. The method marks visited cells and checks if corresponding cells in grid1 are also land. The queue ensures that all connected land cells are processed in layers, moving outward from the initial cell. The BFS approach typically uses more space than DFS due to the need for a queue, which can grow proportionally to the number of cells being processed.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
Other Videos By codestorywithMIK
Other Statistics
Counter-Strike: Source Statistics For codestorywithMIK
At this time, codestorywithMIK has 34,577 views for Counter-Strike: Source spread across 5 videos. Less than an hour worth of Counter-Strike: Source videos were uploaded to his channel, making up less than 0.10% of the total overall content on codestorywithMIK's YouTube channel.