Skip to content

Commit 482e8c2

Browse files
solves problems in python
* 1472. Design Browser History * problem 211: Design Add and Search Words Data Structure * added new lines at end of files * 208: implement Trie (Prefix Tree) * 2348: Number of Zero-Filled Subarrays * 218: The Skyline Problem * 2492: Minimum Score of a Path Between Two Cities * added Links + Time & Space Complexity * 1319: Number of Operations to Make Network Connected * 200: Number of Islands
1 parent 2df36e1 commit 482e8c2

6 files changed

+179
-0
lines changed

Diff for: python/minimum_score_of_a_path_between_two_cities.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/
2+
# T: O(N + M) where N is the number of nodes and M is the number of edges
3+
# S: O(N + M) where N is the number of nodes and M is the number of edges
4+
5+
from collections import deque
6+
7+
class Solution:
8+
def __init__(self):
9+
self.min_dist = 1e14
10+
11+
def minScore(self, n: int, roads) -> int:
12+
graph = {}
13+
vis = [False]*n
14+
for x, y, d in roads:
15+
if x in graph:
16+
graph[x].append((y, d))
17+
else:
18+
graph[x] = [(y, d)]
19+
if y in graph:
20+
graph[y].append((x, d))
21+
else:
22+
graph[y] = [(x, d)]
23+
24+
visited = set()
25+
queue = deque([1])
26+
27+
while queue:
28+
node = queue.popleft()
29+
for adj, score in graph[node]:
30+
if adj not in visited:
31+
queue.append(adj)
32+
visited.add(adj)
33+
self.min_dist = min(self.min_dist, score)
34+
return self.min_dist

Diff for: python/no_of_operations_to_make_network_connected.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# https://leetcode.com/problems/number-of-operations-to-make-network-connected/description/
2+
# T: O(M) where M is the number of connections
3+
# S: O(N) where N is the number of nodes
4+
5+
class Solution:
6+
def makeConnected(self, n, connections) -> int:
7+
if len(connections) < n-1:
8+
return -1
9+
if n == 1:
10+
return 0
11+
graph = {}
12+
for a, b in connections:
13+
if a in graph:
14+
graph[a].append(b)
15+
else:
16+
graph[a] = [b]
17+
18+
if b in graph:
19+
graph[b].append(a)
20+
else:
21+
graph[b] = [a]
22+
23+
visited = [0] * n
24+
25+
def dfs(node):
26+
if visited[node]:
27+
return 0
28+
visited[node] = 1
29+
if node in graph:
30+
for num in graph[node]:
31+
dfs(num)
32+
return 1
33+
34+
35+
return sum(dfs(node) for node in range(n)) - 1

Diff for: python/number_of_islands.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# https://leetcode.com/problems/number-of-islands/description/
2+
# T: O(MN) where M is the number of rows and N is the number of columns.
3+
# S: O(MN) where M is the number of rows and N is the number of columns.
4+
5+
def numIslands(self, grid):
6+
if not grid:
7+
return 0
8+
9+
count = 0
10+
for i in range(len(grid)):
11+
for j in range(len(grid[0])):
12+
if grid[i][j] == '1':
13+
self.dfs(grid, i, j)
14+
count += 1
15+
return count
16+
17+
def dfs(self, grid, i, j):
18+
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != '1':
19+
return
20+
grid[i][j] = '#'
21+
self.dfs(grid, i+1, j)
22+
self.dfs(grid, i-1, j)
23+
self.dfs(grid, i, j+1)
24+
self.dfs(grid, i, j-1)

Diff for: python/number_of_zero_filled_subarrays.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# https://leetcode.com/problems/number-of-zero-filled-subarrays/
2+
# T: O(N) where N is the length of nums
3+
# S: O(1)
4+
5+
class Solution:
6+
def zeroFilledSubarray(self, nums) -> int:
7+
count = 0
8+
i = 0
9+
while i < len(nums):
10+
if nums[i] == 0:
11+
j = i
12+
while j < len(nums) and nums[j] == 0:
13+
j += 1
14+
count += (j - i) * (j - i + 1) // 2
15+
i = j
16+
else:
17+
i = i+1
18+
19+
return count

Diff for: python/the_skyline_problem.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# https://leetcode.com/problems/the-skyline-problem/
2+
# T: O(NlogN) where N is the number of buildings
3+
# S: O(N) where N is the number of buildings
4+
5+
import bisect
6+
7+
class Solution:
8+
def getSkyline(self, buildings):
9+
h = []
10+
for b in buildings:
11+
h.append((b[0], -b[2]))
12+
h.append((b[1], b[2]))
13+
14+
h.sort()
15+
prev = cur = 0
16+
m = [0]
17+
res = []
18+
19+
for i in h:
20+
if i[1] < 0:
21+
bisect.insort(m, -i[1])
22+
else:
23+
m.remove(i[1])
24+
cur = max(m)
25+
if cur != prev:
26+
res.append([i[0], cur])
27+
prev = cur
28+
29+
return res

Diff for: python/trie.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# https://leetcode.com/problems/implement-trie-prefix-tree/
2+
# T: O(N) where N is the length of the word
3+
# S: O(MN) where M is the number of words, N is the maximum length of the word
4+
5+
class TrieNode:
6+
def __init__(self):
7+
self.children = {}
8+
self.is_word = False
9+
10+
class Trie:
11+
12+
def __init__(self):
13+
self.root = TrieNode()
14+
15+
def insert(self, word: str) -> None:
16+
node = self.root
17+
for char in word:
18+
if char not in node.children:
19+
node.children[char] = TrieNode()
20+
node = node.children[char]
21+
node.is_word = True
22+
23+
def search(self, word: str) -> bool:
24+
node = self.root
25+
for char in word:
26+
if char not in node.children:
27+
return False
28+
node = node.children[char]
29+
return node.is_word
30+
31+
32+
def startsWith(self, prefix: str) -> bool:
33+
node = self.root
34+
for char in prefix:
35+
if char not in node.children:
36+
return False
37+
node = node.children[char]
38+
return True

0 commit comments

Comments
 (0)