Pacific Atlantic Water Flow | DFS & BFS | LeetCode 417 | Python, Java & C++ | DSA
RisingBrain
0:00 / 0:00
Pacific Atlantic Water Flow | DFS & BFS | LeetCode 417 | Python, Java & C++ | DSA
848 просмотров · 1 день назад
RisingBrain
41,7 тыс. подписчиков
848 просмотров · 1 день назад
Pacific Atlantic Water Flow is a popular LeetCode 417 problem based on DFS, BFS, Matrix/Grid Traversal and graph traversal concepts.
In this video, we solve the Pacific Atlantic Water Flow problem step by step and understand how to find all cells from which water can flow to both the Pacific Ocean and Atlantic Ocean.
Instead of checking water flow from every cell separately, we use a reverse traversal approach. We start DFS/BFS from the ocean boundaries and move to cells with greater or equal height. The cells reachable from both oceans are our answer.
This problem is an important DFS/BFS grid problem for coding interviews and helps build a strong understanding of matrix traversal, visited arrays, boundary traversal and reverse graph thinking.
The solution is explained with examples and code in Python, Java and C++.
Complexity
Time Complexity: O(M × N)
Space Complexity: O(M × N)
Rising Brain (DSA Sheet):
https://www.risingbrain.org/sheet
Connect with me here:
LinkedIn – / anjalikumari22
Instagram – / rbanjali.codes
Twitter (X) – https://x.com/anjali1kumari?s=21