Skip to content

Commit d975de8

Browse files
committed
added more implementations
1 parent d35bf75 commit d975de8

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

Algorithms/BFS&DFSInBinaryTree.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections import deque
2+
3+
class Node:
4+
def __init__(self, value):
5+
self.value = value
6+
self.left = None
7+
self.right = None
8+
9+
def BFS(root):
10+
if root is None:
11+
return
12+
13+
queue = deque([root])
14+
15+
while queue:
16+
node = queue.popleft() # dequeue a node from the front of the queue
17+
print(node.value, end=' ') # visit the node (print its value in this case)
18+
19+
# enqueue left child
20+
if node.left:
21+
queue.append(node.left)
22+
# enqueue right child
23+
if node.right:
24+
queue.append(node.right)
25+
26+
def DFS(node):
27+
if node is None:
28+
return
29+
# Visit the node (print its value in this case)
30+
print(node.value, end=' ')
31+
# Recursively call DFS on the left child
32+
DFS(node.left)
33+
# Recursively call DFS on the right child
34+
DFS(node.right)
35+
36+
root = Node(1)
37+
root.left = Node(2)
38+
root.right = Node(3)
39+
root.left.left = Node(4)
40+
root.left.right = Node(5)
41+
42+
print("Depth first search: ")
43+
DFS(root)
44+
print("\nBreadth first search: ")
45+
BFS(root)

Data Structures/GraphClass.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Vertex:
2+
def __init__(self, vertex_id):
3+
self.id = vertex_id
4+
self.neighbors = []
5+
6+
def add_neighbor(self, neighbor):
7+
if neighbor not in self.neighbors:
8+
self.neighbors.append(neighbor)
9+
10+
class Graph:
11+
def __init__(self):
12+
self.vertices = {}
13+
14+
def add_vertex(self, vertex):
15+
if isinstance(vertex, Vertex) and vertex.id not in self.vertices:
16+
self.vertices[vertex.id] = vertex
17+
return True
18+
else:
19+
return False
20+
21+
def add_edge(self, v1, v2):
22+
if v1 in self.vertices and v2 in self.vertices:
23+
self.vertices[v1].add_neighbor(v2)
24+
self.vertices[v2].add_neighbor(v1)
25+
return True
26+
else:
27+
return False
28+
29+
def get_vertices(self):
30+
return self.vertices.keys()
31+
32+
def __iter__(self):
33+
return iter(self.vertices.values())
34+
35+
# Create a graph with 4 vertices and 5 edges
36+
graph = Graph()
37+
for i in range(4):
38+
graph.add_vertex(Vertex(i))
39+
graph.add_edge(0, 1)
40+
graph.add_edge(0, 2)
41+
graph.add_edge(1, 2)
42+
graph.add_edge(1, 3)
43+
graph.add_edge(2, 3)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import heapq
2+
3+
4+
class Solution:
5+
def smallestRange(self, nums: list[list[int]]) -> list[int]:
6+
minHeap = [(row[0], i, 0) for i, row in enumerate(nums)]
7+
heapq.heapify(minHeap)
8+
9+
maxRange = max(row[0] for row in nums)
10+
minRange = heapq.nsmallest(1, minHeap)[0][0]
11+
ans = [minRange, maxRange]
12+
13+
while len(minHeap) == len(nums):
14+
num, r, c = heapq.heappop(minHeap)
15+
if c + 1 < len(nums[r]):
16+
heapq.heappush(minHeap, (nums[r][c + 1], r, c + 1))
17+
maxRange = max(maxRange, nums[r][c + 1])
18+
minRange = heapq.nsmallest(1, minHeap)[0][0]
19+
if maxRange - minRange < ans[1] - ans[0]:
20+
ans[0], ans[1] = minRange, maxRange
21+
22+
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from ast import List
2+
import heapq
3+
4+
class solution:
5+
def minGroups(self, intervals: List[List[int]]) -> int:
6+
minHeap = []
7+
8+
for left, right in sorted(intervals):
9+
if minHeap and left > minHeap[0]:
10+
heapq.heappop(minHeap)
11+
heapq.heappush(minHeap, right)
12+
13+
return len(minHeap)

0 commit comments

Comments
 (0)