Skip to content

Commit fad19b6

Browse files
author
Partho Biswas
committed
no message
1 parent 6260abc commit fad19b6

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
859859
|03| [Find_Loop](algoexpert.io/questions/Find_Loop.md) | [Python](algoexpert.io/python/Find_Loop.py)|
860860
|04| [Reverse_Linked_List](algoexpert.io/questions/Reverse_Linked_List.md) | [Python](algoexpert.io/python/Reverse_Linked_List.py)|
861861
|05| [LRU_Cache](algoexpert.io/questions/LRU_Cache.md) | [Python](algoexpert.io/python/LRU_Cache.py)|
862+
|06| [Merge_Linked_List](algoexpert.io/questions/Merge_Linked_List.md) | [Python](algoexpert.io/python/Merge_Linked_List.py)|
862863

863864
</p>
864865
</details>

algoexpert.io/python/Depth_First_Search.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
21
class Node:
3-
42
def __init__(self, name):
53
self.children = []
64
self.name = name
75

8-
def add_child(self, name):
6+
def addChild(self, name):
97
self.children.append(Node(name))
108
return self
119

12-
# O(v + e) time | O(v) space
13-
def depth_first_search(self, array):
10+
def depthFirstSearch(self, array):
1411
array.append(self.name)
1512
for child in self.children:
16-
child.depth_first_search(array)
13+
child.depthFirstSearch(array)
1714
return array
1815

1916

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This is an input class. Do not edit.
2+
class LinkedList:
3+
def __init__(self, value):
4+
self.value = value
5+
self.next = None
6+
7+
8+
def mergeLinkedLists(headOne, headTwo):
9+
p1, p2 = headOne, headTwo
10+
p1Prev = None
11+
while p1 and p2:
12+
if p1.value < p2.value:
13+
p1Prev = p1
14+
p1 = p1.next
15+
else:
16+
if p1Prev:
17+
p1Prev.next = p2
18+
p1Prev = p2
19+
p2 = p2.next
20+
p1Prev.next = p1
21+
if not p1:
22+
p1Prev.next = p2
23+
return headOne if headOne.value < headTwo.value else headTwo
24+
25+
26+

algoexpert.io/python/Min_Heap_Construction.py

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class MinHeap:
5-
65
def __init__(self, array):
76
self.heap = self.build_heap(array)
87

algoexpert.io/python/Water_Area.py

+23
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,27 @@ def waterArea(heights):
1717
else:
1818
maxes[i] = 0
1919
rightMax = max(rightMax, height)
20+
return sum(maxes)
21+
22+
23+
def waterArea(heights):
24+
leftMaxHeights = [0 for _ in heights]
25+
currentLeftMax = 0
26+
for i in range(len(heights)):
27+
height = heights[i]
28+
leftMaxHeights[i] = currentLeftMax
29+
currentLeftMax = max(currentLeftMax, height)
30+
31+
rightMaxHeights = [0 for _ in heights]
32+
currentRightMax = 0
33+
for i in reversed(range(len(heights))):
34+
height = heights[i]
35+
rightMaxHeights[i] = currentRightMax
36+
currentRightMax = max(currentRightMax, height)
37+
38+
maxes = [0 for _ in heights]
39+
for i in range(len(heights)):
40+
minHeight = min(leftMaxHeights[i], rightMaxHeights[i])
41+
if minHeight > heights[i]:
42+
maxes[i] = minHeight - heights[i]
2043
return sum(maxes)

0 commit comments

Comments
 (0)