Skip to content

Commit 74cdf94

Browse files
author
Partho Biswas
committed
947_Most_Stones_Removed_with_Same_Row_or_Column
1 parent 1260f9d commit 74cdf94

File tree

4 files changed

+127
-65
lines changed

4 files changed

+127
-65
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
498498
|26| **[752. Open the Lock](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/752_Open_the_Lock.py)| **[Art 1](https://tinyurl.com/s3oskv8)**, **[Art 2](https://tinyurl.com/rsr4tz9)** | Medium |📌 **TODO: CHECK again. Very important and interesting problem. Loved it** |
499499
|27| **[489. Robot Room Cleaner](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/489_Robot_Room_Cleaner.py), [Swift](https://tinyurl.com/wuja3c4/489_Robot_Room_Cleaner.swift) | **[Vid 1](https://tinyurl.com/vahalvm)**, **[Vid 2](https://tinyurl.com/qpumkro)**, **[Art 1](https://tinyurl.com/rnu8679)** | Hard |📌 **TODO: CHECK again. Very important and interesting problem. I fucking loved it** |
500500
|28| **[1136. Parallel Courses](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/11136_Parallel_Courses.py), [Swift](https://tinyurl.com/wuja3c4/11136_Parallel_Courses.swift) | --- | Hard |📌 Topological sort. |
501+
|29| **[947. Most Stones Removed with Same Row or Column](https://tinyurl.com/rrm6w2a)** | [Python](https://tinyurl.com/wu6rdaw/947_Most_Stones_Removed_with_Same_Row_or_Column.py), [Swift](https://tinyurl.com/wuja3c4/947_Most_Stones_Removed_with_Same_Row_or_Column.swift) | [Vid 1](https://tinyurl.com/sc2g6eg), [Art 1](https://tinyurl.com/sxyzxkr), [Vid 2](https://tinyurl.com/wocnrhn), [Art 2](https://tinyurl.com/sogsj74), [Art 3](https://tinyurl.com/w4pk463) | Medium |📌 DFS and Union FInd |
501502

502503

503504
</p>

leetcode.com/python/200_Number_of_Islands.py

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Solution(object):
77
# Class to represent
88
# Disjoint Set Data structure
99
class DisjointUnionSets(object):
10+
1011
def __init__(self, n):
1112
self.rank = [0] * n
1213
self.parent = [0] * n
@@ -20,9 +21,7 @@ def makeSet(self):
2021
for i in range(self.n):
2122
self.parent[i] = i
2223

23-
# Finds the representative of the set that x
24-
25-
# is an element of
24+
# Finds the representative of the set that x is an element of
2625
def find(self, x):
2726
if (self.parent[x] != x):
2827
# if x is not the parent of itself,
@@ -34,9 +33,7 @@ def find(self, x):
3433
return self.find(self.parent[x])
3534
return x
3635

37-
# Unites the set that includes x and
38-
39-
# the set that includes y
36+
# Unites the set that includes x and the set that includes y
4037
def Union(self, x, y):
4138

4239
# Find the representatives(or the root nodes)
@@ -60,7 +57,6 @@ def Union(self, x, y):
6057
# remains less
6158
elif self.rank[yRoot] < self.rank[xRoot]:
6259
self.parent[yRoot] = xRoot
63-
6460
else:
6561

6662
# Else if their ranks are the same
@@ -72,6 +68,7 @@ def Union(self, x, y):
7268
# rank by 1
7369
self.rank[xRoot] = self.rank[xRoot] + 1
7470

71+
7572
# Returns number of islands in a[][]
7673
def numIslands(self, grid):
7774
"""
@@ -134,56 +131,56 @@ def numIslands(self, grid):
134131

135132

136133

137-
# # Approach 2: Using DFS - Accepted
138-
# class Solution(object):
139-
# def numIslands(self, grid):
140-
# """
141-
# :type grid: List[List[str]]
142-
# :rtype: int
143-
# """
144-
# sizes = []
145-
# visited = [[False for value in row] for row in grid]
146-
# for i in range(len(grid)):
147-
# for j in range(len(grid[i])):
148-
# if visited[i][j]:
149-
# continue
150-
# else:
151-
# self.traverseNode(i, j, grid, visited, sizes)
152-
# return len(sizes)
153-
#
154-
#
155-
# def traverseNode(self, i, j, grid, visited, sizes):
156-
# currentRiverSize = 0
157-
# nodesToExplore = [[i, j]]
158-
# while len(nodesToExplore):
159-
# currentNode = nodesToExplore.pop()
160-
# i = currentNode[0]
161-
# j = currentNode[1]
162-
# if visited[i][j]:
163-
# continue
164-
# visited[i][j] = True
165-
# if grid[i][j] == "0":
166-
# continue
167-
# currentRiverSize += 1
168-
# unvisitedNeighbours = self.getUnvisitedNeighbour(i, j, grid, visited)
169-
# for neighbour in unvisitedNeighbours:
170-
# nodesToExplore.append(neighbour)
171-
# if currentRiverSize > 0:
172-
# sizes.append(currentRiverSize)
173-
#
174-
#
175-
#
176-
# def getUnvisitedNeighbour(self, i, j, grid, visited):
177-
# unvisitedNeighbours = []
178-
# if i > 0 and not visited[i - 1][j]:
179-
# unvisitedNeighbours.append([i - 1, j])
180-
# if i < len(grid) - 1 and not visited[i + 1][j]:
181-
# unvisitedNeighbours.append([i + 1, j])
182-
# if j > 0 and not visited[i][j - 1]:
183-
# unvisitedNeighbours.append([i, j - 1])
184-
# if j < len(grid[0]) - 1 and not visited[i][j + 1]:
185-
# unvisitedNeighbours.append([i, j + 1])
186-
# return unvisitedNeighbours
134+
# Approach 2: Using DFS - Accepted
135+
class Solution(object):
136+
def numIslands(self, grid):
137+
"""
138+
:type grid: List[List[str]]
139+
:rtype: int
140+
"""
141+
sizes = []
142+
visited = [[False for value in row] for row in grid]
143+
for i in range(len(grid)):
144+
for j in range(len(grid[i])):
145+
if visited[i][j]:
146+
continue
147+
else:
148+
self.traverseNode(i, j, grid, visited, sizes)
149+
return len(sizes)
150+
151+
152+
def traverseNode(self, i, j, grid, visited, sizes):
153+
currentRiverSize = 0
154+
nodesToExplore = [[i, j]]
155+
while len(nodesToExplore):
156+
currentNode = nodesToExplore.pop()
157+
i = currentNode[0]
158+
j = currentNode[1]
159+
if visited[i][j]:
160+
continue
161+
visited[i][j] = True
162+
if grid[i][j] == "0":
163+
continue
164+
currentRiverSize += 1
165+
unvisitedNeighbours = self.getUnvisitedNeighbour(i, j, grid, visited)
166+
for neighbour in unvisitedNeighbours:
167+
nodesToExplore.append(neighbour)
168+
if currentRiverSize > 0:
169+
sizes.append(currentRiverSize)
170+
171+
172+
173+
def getUnvisitedNeighbour(self, i, j, grid, visited):
174+
unvisitedNeighbours = []
175+
if i > 0 and not visited[i - 1][j]:
176+
unvisitedNeighbours.append([i - 1, j])
177+
if i < len(grid) - 1 and not visited[i + 1][j]:
178+
unvisitedNeighbours.append([i + 1, j])
179+
if j > 0 and not visited[i][j - 1]:
180+
unvisitedNeighbours.append([i, j - 1])
181+
if j < len(grid[0]) - 1 and not visited[i][j + 1]:
182+
unvisitedNeighbours.append([i, j + 1])
183+
return unvisitedNeighbours
187184

188185

189186

leetcode.com/python/305_Number_of_Islands_II.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Solution(object):
88
# Class to represent
99
# Disjoint Set Data structure
1010
class DisjointUnionSets(object):
11+
1112
def __init__(self, n):
1213
self.rank = [0] * n
1314
self.parent = [0] * n
@@ -21,9 +22,7 @@ def makeSet(self):
2122
for i in range(self.n):
2223
self.parent[i] = i
2324

24-
# Finds the representative of the set that x
25-
26-
# is an element of
25+
# Finds the representative of the set that x is an element of
2726
def find(self, x):
2827
if (self.parent[x] != x):
2928
# if x is not the parent of itself,
@@ -35,9 +34,7 @@ def find(self, x):
3534
return self.find(self.parent[x])
3635
return x
3736

38-
# Unites the set that includes x and
39-
40-
# the set that includes y
37+
# Unites the set that includes x and the set that includes y
4138
def Union(self, x, y):
4239

4340
# Find the representatives(or the root nodes)
@@ -61,7 +58,6 @@ def Union(self, x, y):
6158
# remains less
6259
elif self.rank[yRoot] < self.rank[xRoot]:
6360
self.parent[yRoot] = xRoot
64-
6561
else:
6662

6763
# Else if their ranks are the same
@@ -118,7 +114,6 @@ def numIslands2(self, m, n, positions):
118114

119115

120116
# SOURCE: https://tinyurl.com/qohtl3m
121-
122117
class UnionFind(object):
123118
def __init__(self):
124119
self.parents = {}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Source: https://tinyurl.com/sxyzxkr
2+
# Approach 1: DFS
3+
class Solution(object):
4+
def removeStones(self, stones):
5+
"""
6+
:type stones: List[List[int]]
7+
:rtype: int
8+
"""
9+
if len(stones) < 2:
10+
return 0
11+
disJointSetCount, visited = 0, set()
12+
for x, y in stones:
13+
if (x, y) not in visited:
14+
self.DFS(stones, visited, x, y)
15+
disJointSetCount += 1
16+
return len(stones) - disJointSetCount
17+
18+
def DFS(self, stones, visited, x, y):
19+
if (x, y) not in visited:
20+
visited.add((x, y))
21+
for nextX, nextY in stones:
22+
if x == nextX:
23+
self.DFS(stones, visited, x, nextY)
24+
if y == nextY:
25+
self.DFS(stones, visited, nextX, y)
26+
27+
28+
29+
30+
# Approach 2: Union Find
31+
class Solution(object):
32+
33+
class DisjointUnionSets(object):
34+
def __init__(self, numberOfSets):
35+
self.rank = [0 for _ in range(numberOfSets)]
36+
self.parent = [i for i in range(numberOfSets)]
37+
self.numberOfSets = numberOfSets
38+
39+
def find(self, setElement):
40+
parentOfSetElement = self.parent[setElement]
41+
if parentOfSetElement != setElement:
42+
return self.find(parentOfSetElement)
43+
return parentOfSetElement
44+
45+
def union(self, elementA, elementB):
46+
parentA, parentB = self.find(elementA), self.find(elementB)
47+
if parentA == parentB:
48+
return
49+
if self.rank[parentA] >= self.rank[parentB]:
50+
self.parent[parentB] = parentA
51+
self.rank[parentA] += self.rank[parentB]
52+
else:
53+
self.parent[parentA] = parentB
54+
self.parent[parentB] += self[parentA]
55+
self.numberOfSets -= 1
56+
57+
def removeStones(self, stones):
58+
"""
59+
:type stones: List[List[int]]
60+
:rtype: int
61+
"""
62+
numberOfStones = len(stones)
63+
disjointSet = self.DisjointUnionSets(numberOfStones)
64+
for i in range(len(numberOfStones)):
65+
for j in range(i + 1, numberOfStones):
66+
if (stones[i][1] == stones[j][1]) or (stones[i][0] == stones[j][0]):
67+
disjointSet.union(i, j)
68+
69+
return numberOfStones - disjointSet.numberOfSets

0 commit comments

Comments
 (0)