Skip to content

Commit 891b50d

Browse files
committed
no message
1 parent b744b87 commit 891b50d

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Max stones can be remove = number of stones - number of groups
3+
The stone in the same group has the same row or col with any stone in the group.
4+
Since finding stones in the same group will take O(N^2)
5+
Instead, we union the row or col itself.
6+
7+
So initially each col (c1, c2...) and row (r1, r2...)'s parent is itself.
8+
We iterate through the stones and union the col and row.
9+
For example, the stone (1, 2) will union r2 and c1.
10+
the stone (0, 3) will union r3 and c0.
11+
...
12+
13+
This way, we can also get the number of groups.
14+
15+
See better explanation in https://www.youtube.com/watch?v=beOCN7G4h-M
16+
17+
Time: O(N).
18+
Space: O(N)
19+
"""
20+
class Solution(object):
21+
def removeStones(self, stones):
22+
def union(x, y):
23+
row = 'r'+str(x)
24+
col = 'c'+str(y)
25+
p1 = find(row)
26+
p2 = find(col)
27+
if p1==p2: return False
28+
parents[p2] = p1
29+
return True
30+
31+
def find(c):
32+
p = parents[c]
33+
while p!=parents[p]:
34+
p = find(p)
35+
parents[c] = p
36+
return p
37+
38+
parents = {}
39+
for x, y in stones:
40+
row = 'r'+str(x)
41+
col = 'c'+str(y)
42+
parents[row] = row
43+
parents[col] = col
44+
45+
groupCount = len(parents)
46+
for x, y in stones:
47+
if union(x, y): groupCount -= 1
48+
49+
return len(stones)-groupCount

problems/network-delay-time.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,52 @@ def networkDelayTime(self, times, N, K):
7878

7979
return -1
8080

81+
82+
83+
84+
85+
86+
87+
class Solution(object):
88+
def networkDelayTime(self, times, n, k):
89+
G = collections.defaultdict(list) #adjacency list
90+
D = [float('inf')]*(n+1) #D[n] store the distance between n and k
91+
D[k] = 0
92+
h = [(0, k)]
93+
visited = set()
94+
95+
#form the djacency list
96+
for u, v, w in times:
97+
G[u].append((v, w))
98+
99+
#dijkstra
100+
while h:
101+
d1, node = heapq.heappop(h)
102+
if node in visited: continue
103+
visited.add(node)
104+
105+
for nei, d2 in G[node]:
106+
if d1+d2<D[nei]:
107+
D[nei] = d1+d2
108+
heapq.heappush(h, (D[nei], nei))
109+
110+
#iterate through the distance between n and k, find ans
111+
ans = float('-inf')
112+
for i, d in enumerate(D):
113+
if i==0: continue
114+
if d==float('inf'): return -1
115+
ans = max(ans, d)
116+
return ans
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+

problems/number-of-connected-components-in-an-undirected-graph.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,31 @@ def dfs(start):
2020
g[n2].append(n1)
2121

2222
for n in xrange(N):
23-
# print visited
2423
if n in visited: continue
2524
dfs(n)
2625
count += 1
2726

27+
return count
28+
29+
30+
class Solution(object):
31+
def countComponents(self, n, edges):
32+
def find(n):
33+
p = parents[n]
34+
while p!=parents[p]:
35+
p = find(p)
36+
parents[n] = p
37+
return p
38+
39+
def union(n1, n2):
40+
p1, p2 = find(n1), find(n2)
41+
if p1==p2: return False
42+
parents[p2] = p1
43+
return True
44+
45+
count = n
46+
parents = [n for n in xrange(n)]
47+
48+
for n1, n2 in edges:
49+
if union(n1, n2): count -= 1
2850
return count

problems/number-of-islands-ii.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
For each new land, the count will -= ((number of neighbors) - 1).
3+
Because some for this new land introduced, some of the separate islands will be connected.
4+
Note that, "number of neighbors", islands connected does not count. So for each neighbor, we use "find" to find its root.
5+
"""
6+
class Solution(object):
7+
def numIslands2(self, M, N, positions):
8+
def coorToNum(r, c):
9+
return N*r+c
10+
11+
def find(n):
12+
p = parents[n]
13+
while p!=parents[p]:
14+
p = find(p)
15+
parents[n] = p
16+
return parents[n]
17+
18+
def union(n1, n2):
19+
p1 = find(n1)
20+
p2 = find(n2)
21+
22+
if p1==p2: return
23+
parents[p1] = p2
24+
25+
count = 0
26+
lands = set()
27+
ans = []
28+
parents = [n for n in xrange(M*N)]
29+
30+
for r, c in positions:
31+
n = coorToNum(r, c)
32+
33+
if n in lands:
34+
ans.append(count)
35+
continue
36+
37+
neis = set()
38+
if r+1<M and coorToNum(r+1, c) in lands: neis.add(find(coorToNum(r+1, c)))
39+
if 0<=r-1 and coorToNum(r-1, c) in lands: neis.add(find(coorToNum(r-1, c)))
40+
if c+1<N and coorToNum(r, c+1) in lands: neis.add(find(coorToNum(r, c+1)))
41+
if 0<=c-1 and coorToNum(r, c-1) in lands: neis.add(find(coorToNum(r, c-1)))
42+
43+
count -= (len(neis)-1)
44+
lands.add(n)
45+
ans.append(count)
46+
47+
for nei in neis:
48+
union(n, nei)
49+
50+
return ans

0 commit comments

Comments
 (0)