Skip to content

Added Three Solutions in python #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 24, 2023
34 changes: 34 additions & 0 deletions python/minimum_score_of_a_path_between_two_cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/
# T: O(N + M) where N is the number of nodes and M is the number of edges
# S: O(N + M) where N is the number of nodes and M is the number of edges

from collections import deque

class Solution:
def __init__(self):
self.min_dist = 1e14

def minScore(self, n: int, roads) -> int:
graph = {}
vis = [False]*n
for x, y, d in roads:
if x in graph:
graph[x].append((y, d))
else:
graph[x] = [(y, d)]
if y in graph:
graph[y].append((x, d))
else:
graph[y] = [(x, d)]

visited = set()
queue = deque([1])

while queue:
node = queue.popleft()
for adj, score in graph[node]:
if adj not in visited:
queue.append(adj)
visited.add(adj)
self.min_dist = min(self.min_dist, score)
return self.min_dist
35 changes: 35 additions & 0 deletions python/no_of_operations_to_make_network_connected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# https://leetcode.com/problems/number-of-operations-to-make-network-connected/description/
# T: O(M) where M is the number of connections
# S: O(N) where N is the number of nodes

class Solution:
def makeConnected(self, n, connections) -> int:
if len(connections) < n-1:
return -1
if n == 1:
return 0
graph = {}
for a, b in connections:
if a in graph:
graph[a].append(b)
else:
graph[a] = [b]

if b in graph:
graph[b].append(a)
else:
graph[b] = [a]

visited = [0] * n

def dfs(node):
if visited[node]:
return 0
visited[node] = 1
if node in graph:
for num in graph[node]:
dfs(num)
return 1


return sum(dfs(node) for node in range(n)) - 1
24 changes: 24 additions & 0 deletions python/number_of_islands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# https://leetcode.com/problems/number-of-islands/description/
# T: O(MN) where M is the number of rows and N is the number of columns.
# S: O(MN) where M is the number of rows and N is the number of columns.

def numIslands(self, grid):
if not grid:
return 0

count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
self.dfs(grid, i, j)
count += 1
return count

def dfs(self, grid, i, j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != '1':
return
grid[i][j] = '#'
self.dfs(grid, i+1, j)
self.dfs(grid, i-1, j)
self.dfs(grid, i, j+1)
self.dfs(grid, i, j-1)
19 changes: 19 additions & 0 deletions python/number_of_zero_filled_subarrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# https://leetcode.com/problems/number-of-zero-filled-subarrays/
# T: O(N) where N is the length of nums
# S: O(1)

class Solution:
def zeroFilledSubarray(self, nums) -> int:
count = 0
i = 0
while i < len(nums):
if nums[i] == 0:
j = i
while j < len(nums) and nums[j] == 0:
j += 1
count += (j - i) * (j - i + 1) // 2
i = j
else:
i = i+1

return count
29 changes: 29 additions & 0 deletions python/the_skyline_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# https://leetcode.com/problems/the-skyline-problem/
# T: O(NlogN) where N is the number of buildings
# S: O(N) where N is the number of buildings

import bisect

class Solution:
def getSkyline(self, buildings):
h = []
for b in buildings:
h.append((b[0], -b[2]))
h.append((b[1], b[2]))

h.sort()
prev = cur = 0
m = [0]
res = []

for i in h:
if i[1] < 0:
bisect.insort(m, -i[1])
else:
m.remove(i[1])
cur = max(m)
if cur != prev:
res.append([i[0], cur])
prev = cur

return res
38 changes: 38 additions & 0 deletions python/trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# https://leetcode.com/problems/implement-trie-prefix-tree/
# T: O(N) where N is the length of the word
# S: O(MN) where M is the number of words, N is the maximum length of the word

class TrieNode:
def __init__(self):
self.children = {}
self.is_word = False

class Trie:

def __init__(self):
self.root = TrieNode()

def insert(self, word: str) -> None:
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_word = True

def search(self, word: str) -> bool:
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_word


def startsWith(self, prefix: str) -> bool:
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]
return True