Submission Detail

49 / 49 test cases passed.
Status:

Accepted

Runtime: 711 ms
Memory Usage: 28.6 MB
Submitted: 0 minutes ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

200
300
400
500
600
700
800
900
1000
1100
1200
1300
0.5
1.0
1.5
2.0
python
You are here!
Your runtime beats 16.56 % of python submissions.
Runtime (ms)
Distribution (%)

200
300
400
500
600
700
800
900
1000
1100
1200
1300
1.0
2.0
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

27000
28000
29000
30000
31000
32000
33000
34000
35000
5
10
15
20
python
You are here!
Your memory usage beats 93.74 % of python submissions.
Memory (KB)
Distribution (%)

27000
28000
29000
30000
31000
32000
33000
34000
35000
5
10
15
20
Zoom area by dragging across this chart

Invite friends to challenge Number of Islands


Submitted Code: 0 minutes ago

Language: python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
islands = 0
ones = []
for r in range(0,len(grid)):
for c in range(0,len(grid[r])):
if (grid[r][c] == "1"):
ones.append((r,c))
queue = []
while (len(ones) > 0):
queue.append(ones[0])
while (queue != []):
n = queue.pop(0)
# check neighbors
# left
if (n[1] - 1 >= 0):
if (grid[n[0]][n[1] - 1] == "1" and (n[0],n[1] - 1) not in queue):
queue.append((n[0],n[1]-1))
# right
if (n[1] + 1 < len(grid[0])):
if (grid[n[0]][n[1] + 1] == "1" and (n[0],n[1]+1) not in queue):
queue.append((n[0],n[1]+1))
# up
if (n[0] - 1 >= 0):
if (grid[n[0]-1][n[1]] == "1" and (n[0]-1,n[1]) not in queue):
queue.append((n[0]-1,n[1]))
# down
if (n[0] + 1 < len(grid)):
if (grid[n[0]+1][n[1]] == "1" and (n[0]+1,n[1]) not in queue):
queue.append((n[0]+1,n[1]))
grid[n[0]][n[1]] = "0"
ones.remove((n[0],n[1]))
islands += 1
return islands
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX