Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
leetcode
Manage
Activity
Members
Labels
Plan
Issues
29
Issue boards
Milestones
Code
Merge requests
29
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
John bedingfield
leetcode
Commits
183dbd0e
Commit
183dbd0e
authored
2 years ago
by
John bedingfield
Browse files
Options
Downloads
Patches
Plain Diff
solution to LeetCode 200 Number of Islands via bfs
parent
27329d3a
No related branches found
Branches containing commit
Tags
v1.1.0
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
200_islands_bfs_noVisitedSet.py
+91
-0
91 additions, 0 deletions
200_islands_bfs_noVisitedSet.py
with
91 additions
and
0 deletions
200_islands_bfs_noVisitedSet.py
0 → 100644
+
91
−
0
View file @
183dbd0e
# -*- 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
"
],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ttime
=
int
(
time
())
print
(
f
"
Number of islands:
{
s
.
numIslands
(
grid2
)
}
"
)
endtime
=
int
(
time
())
totalTime
=
endtime
-
starttime
print
(
f
"
Total time was
{
totalTime
}
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment