Skip to content

Commit 67a48cb

Browse files
author
Partho Biswas
committed
694. Number of Distinct Islands
1 parent 81e3f5b commit 67a48cb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
540540
|31| **[994_Rotting_Oranges](https://tinyurl.com/yavrnvu5)** | [Python](https://tinyurl.com/wu6rdaw/994_Rotting_Oranges.py), [Swift](https://tinyurl.com/wuja3c4/994_Rotting_Oranges.swift) | --- | Medium |📌 BFS |
541541
|32| **[909. Snakes and Ladders](https://tinyurl.com/yazkm38f)** | [Python](https://tinyurl.com/wu6rdaw/909_Snakes_and_Ladders.py), [Swift](https://tinyurl.com/wuja3c4/909_Snakes_and_Ladders.swift) | [Art 1](https://tinyurl.com/ya45spcc), [Art 2](https://tinyurl.com/ycafdfr4), [Art 3](https://tinyurl.com/yc3hdaw9), [Vid 1](https://tinyurl.com/yc3hdaw9), [Vid 2](https://tinyurl.com/y99hrfvk), | Medium |📌 BFS. Check again |
542542
|33| **[1192. Critical Connections in a Network](https://tinyurl.com/y6u2p7qw)** | [Python](https://tinyurl.com/wu6rdaw/1192_Critical_Connections_in_a_Network.py), [Swift](https://tinyurl.com/wuja3c4/1192_Critical_Connections_in_a_Network.swift) | [Art 1](https://tinyurl.com/yc2tzb4k), [Art 2](https://tinyurl.com/ycjd3rrr), [Art 3](https://tinyurl.com/ybj5nux8), [Art 4](https://tinyurl.com/y9wz5w8o), [Vid 1](https://tinyurl.com/y76cux54), [Vid 2](https://tinyurl.com/ybmgmv5l), [Vid 3](https://tinyurl.com/y7sa5oou) | Hard |📌 **Important, Learned new things.Tarjans SCCs algorithm. Check again** |
543+
|34| **[694. Number of Distinct Islands](https://tinyurl.com/ybf8cedn)** | [Python](https://tinyurl.com/wu6rdaw/694_Number_of_Distinct_Islands.py), [Swift](https://tinyurl.com/wuja3c4/694_Number_of_Distinct_Islands.swift) | [Art 1](https://tinyurl.com/y7rapyk6), [Art 2](https://tinyurl.com/yde8dqbb) | Medium |📌 |
543544

544545

545546
</p>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def numDistinctIslands(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
distinctIlandSet = set()
8+
rows, cols = len(grid), len(grid[0])
9+
for row in range(rows):
10+
for col in range(cols):
11+
if grid[row][col] == 1:
12+
currentIlanndPosition = []
13+
self.numDistinctIslandsDFSHelper(grid, currentIlanndPosition, row, col, 0, 0)
14+
distinctIlandSet.add(tuple(currentIlanndPosition))
15+
return len(distinctIlandSet)
16+
17+
def numDistinctIslandsDFSHelper(self, grid, currentIlanndPosition, row, col, referrenceRowPos, referrenceColPos):
18+
grid[row][col] = -1 # marking this cell as visited
19+
currentIlanndPosition.append((referrenceRowPos, referrenceColPos))
20+
neighbours = [(-1, 0), (0, 1), (1, 0), (0, -1)]
21+
for neighbour in neighbours:
22+
dr, dc = neighbour
23+
nextR, nextC = row + dr, col + dc
24+
nextReferrenceRowPos, nextReferrenceColPos = referrenceRowPos + dr, referrenceColPos + dc
25+
if 0 <= nextR < len(grid) and 0 <= nextC < len(grid[0]) and grid[nextR][nextC] == 1:
26+
self.numDistinctIslandsDFSHelper(grid, currentIlanndPosition, nextR, nextC, nextReferrenceRowPos,
27+
nextReferrenceColPos)
28+

0 commit comments

Comments
 (0)