Skip to content
Snippets Groups Projects
Commit 183dbd0e authored by John bedingfield's avatar John bedingfield
Browse files

solution to LeetCode 200 Number of Islands via bfs

parent 27329d3a
No related branches found
Tags v1.1.0
No related merge requests found
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 15 11:04:07 2022
@author: John Bedingfield
"""
# note: this solution was accepted for LeetCode #200: Number of Islands
# Runtime: 451 ms, faster than 39.83% of python3 submissions
# Memory Usage: 16.4MB, less than 62.10% of python3 submissions
from collections import deque # need a queue for bfs
from time import time # to capture timing data
# ====== start pasting from here =======
class Solution:
def numIslands(self, grid) -> int:
rows = len(grid) # number of rows
cols = len(grid[0]) # number of cols
numOfIslands = 0 # variable for total number of islands
# helper arrays to calculate: up, down, right, left
dX = [-1, 0, 1, 0] # rows
dY = [0, 1, 0, -1] # cols
''' Breadth First Search: takes (row, col) and completes a breadth first search
replaces the "1"s with "0"s as it goes (Carlos's idea),
(this eliminates the need to create a 'visited' set
to keep track of elements already visited)
'''
def bfs(x, y):
q = deque() # bfs accomplished by using a queue (FIFO)
q.append((x,y)) # add this element to the queue
while q: # while there are still elements to be searched
thisOne = q.popleft() # get the next one from the queue
thisX, thisY = thisOne[0], thisOne[1] # get x and y
if grid[thisX][thisY] == "1": # ignore dupes in the queue
grid[thisX][thisY] = "0" # mark as visited
# check up, right, down, left from thisOne
for delta in range(4):
nextOneX = thisX + dX[delta] # calc new x (row)
nextOneY = thisY + dY[delta] # calc new y (col)
# check to ensure it's not already visited, it's a 1, and it's inbounds
if (nextOneX in range(rows) and
nextOneY in range(cols) and
grid[nextOneX][nextOneY] == "1"):
# if so, it's part of the island, we want to search it, add to queue
q.append((nextOneX, nextOneY))
return # end of bfs
''' loop through each element in the grid,
if it's a "1" (land), it's a new island
increase the count, and conduct bfs to determine
the contours of the island
'''
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1":
bfs(r,c)
numOfIslands = numOfIslands + 1
return numOfIslands # end numIslands
# ====== stop pasting here =======
if __name__ == "__main__":
grid1 = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
grid2 = [["1","1","1","1","1","0","1","1","1","1","1","1","1","1","1","0","1","0","1","1"],["0","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1","1","0"],["1","0","1","1","1","0","0","1","1","0","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","0","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","0","0","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","0","1","1","1","1","1","1","0","1","1","1","0","1","1","1","0","1","1","1"],["0","1","1","1","1","1","1","1","1","1","1","1","0","1","1","0","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1","0","1","1"],["1","1","1","1","1","1","1","1","1","1","0","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["0","1","1","1","1","1","1","1","0","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","0","1","1","1","1","1","1","1","0","1","1","1","1","1","1"],["1","0","1","1","1","1","1","0","1","1","1","0","1","1","1","1","0","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1","1","1","0"],["1","1","1","1","1","1","1","1","1","1","1","1","1","0","1","1","1","1","0","0"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"]]
s = Solution()
starttime = int(time())
print(f"Number of islands: {s.numIslands(grid2)}")
endtime = int(time())
totalTime = endtime - starttime
print(f"Total time was {totalTime}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment