Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0929ca2

Browse files
doc: add time and space complexities and reasoning
1 parent 97ee638 commit 0929ca2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎graphs/best_bridge/best_bridge.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,19 @@ def inbounds(grid: List[List], r: int, c: int) -> bool:
102102
col_inbounds = 0 <= c < len(grid[0])
103103

104104
return row_inbounds and col_inbounds
105+
106+
"""
107+
Time and Space Complexities:
108+
109+
- Time Complexity: O(r * c) where r is the number of rows and c is the number of columns in the grid. This is because each cell is processed a constant number of times.
110+
- Space Complexity: O(r * c) for the visited set and the queue, as we might need to store all the cells in the worst case.
111+
112+
Further Notes:
113+
- Approach:
114+
- The algorithm uses Depth-First Search (DFS) to identify the first island.
115+
- Breadth-First Search (BFS) is then used to find the shortest bridge to another island by traversing water cells.
116+
- The BFS ensures that the shortest path is found due to its level-wise exploration.
117+
- Reasoning:
118+
- Starting with DFS helps in identifying all connected cells of the first island.
119+
- BFS is optimal for finding the shortest path in an unweighted grid, which is why it is used after identifying the first island.
120+
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /