Skip to content

Commit 7fabde9

Browse files
author
Partho Biswas
committed
no message
1 parent 25538c3 commit 7fabde9

7 files changed

+203
-51
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ I have solved quite a number of problems from several topics. See the below tabl
218218
|32| [788. Rotated Digits](https://tinyurl.com/t9gelfm) | [Python](https://tinyurl.com/wu6rdaw/788_Rotated_Digits.py), [Swift](https://tinyurl.com/wuja3c4/788_Rotated_Digits.swift) | --- | Easy | --- |
219219
|33| [388. Longest Absolute File Path](https://tinyurl.com/svfa3rc) | [Python](https://tinyurl.com/wu6rdaw/388_Longest_Absolute_File_Path.py), [Swift](https://tinyurl.com/wuja3c4/388_Longest_Absolute_File_Path.swift) | --- | Medium | --- |
220220
|34| [616. Add Bold Tag in String](https://tinyurl.com/taftav2) | [Python](https://tinyurl.com/wu6rdaw/616_Add_Bold_Tag_in_String.py), [Swift](https://tinyurl.com/wuja3c4/616_Add_Bold_Tag_in_String.swift) | --- | Medium | Merge intervals(hidden) |
221+
|35| **[678. Valid Parenthesis String](https://tinyurl.com/y9a3g9kq)** | [Python](https://tinyurl.com/wu6rdaw/678_Valid_Parenthesis_String.py), [Swift](https://tinyurl.com/wuja3c4/678_Valid_Parenthesis_String.swift) | [Art 1](https://tinyurl.com/yad2akx6), [Art 2](https://tinyurl.com/y4odwzll), [Art 3](https://tinyurl.com/y7lkgfom), [Art 4](https://tinyurl.com/ycxpeyzn) | Medium | Super tricky. Much check again |
221222

222223

223224
</p>

algoexpert.io/python/BST_Construction_Recursive.py

+123-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# https://www.algoexpert.io/questions/BST%20Construction
2-
1+
# My solution. Failing on 2 test case but do't know why
32
class BST:
43
def __init__(self, value):
54
self.left = None
@@ -21,15 +20,15 @@ def insert(self, value):
2120

2221
def contains(self, value):
2322
if value < self.value:
24-
if self.value is None:
23+
if self.left is None:
2524
return False
2625
else:
2726
self.left.contains(value)
2827
elif value > self.value:
29-
if self.right is None:
30-
return False
31-
else:
32-
self.right.contains(value)
28+
if self.right is None:
29+
return False
30+
else:
31+
return self.right.contains(value)
3332
else:
3433
return True
3534

@@ -54,7 +53,7 @@ def remove(self, value, parent = None):
5453
self.left = self.right.left
5554
self.right = self.right.right
5655
else:
57-
self.value = None
56+
pass # This is a single-node tree. nothing to do
5857
elif parent.left == self:
5958
parent.left = self.left if self.left is not None else self.right
6059
elif parent.right == self:
@@ -67,3 +66,119 @@ def get_min_value(self):
6766
else:
6867
return self.left.get_min_value()
6968

69+
70+
71+
72+
# Source: https://tinyurl.com/rxnq4sz
73+
def traverse_list(node):
74+
visit_order = list()
75+
if node:
76+
visit_order.append(node.value)
77+
visit_order += traverse_list(node.left)
78+
visit_order += traverse_list(node.right)
79+
return visit_order
80+
81+
82+
def traverse(node):
83+
visit_order = list()
84+
if node:
85+
visit_order.append(node)
86+
visit_order += traverse(node.left)
87+
visit_order += traverse(node.right)
88+
return visit_order
89+
90+
91+
def get_min_node_value(node):
92+
while node.left:
93+
node = node.left
94+
return node.value
95+
96+
97+
class BST:
98+
def __init__(self, value):
99+
self.value = value
100+
self.left = None
101+
self.right = None
102+
103+
def compare(self, target):
104+
if self.value > target:
105+
return -1
106+
elif self.value < target:
107+
return 1
108+
else:
109+
return 0
110+
111+
def insert(self, value):
112+
113+
node = self
114+
while True:
115+
comparision = node.compare(value)
116+
if comparision == -1:
117+
if node.left:
118+
node = node.left
119+
else:
120+
node.left = BST(value)
121+
break
122+
else: # comparision == 1 or equals
123+
if node.right:
124+
node = node.right
125+
else:
126+
node.right = BST(value)
127+
break
128+
129+
return self
130+
131+
def contains(self, value):
132+
node = self
133+
while node:
134+
comparision = node.compare(value)
135+
if comparision == -1:
136+
node = node.left
137+
elif comparision == 1:
138+
node = node.right
139+
else:
140+
return True
141+
142+
return False
143+
144+
def remove(self, value, parent_node=None):
145+
node = self
146+
while True:
147+
comparision = node.compare(value)
148+
if comparision == -1:
149+
if node.left:
150+
parent_node = node
151+
node = node.left
152+
else:
153+
print('Value not found')
154+
break
155+
elif comparision == 1:
156+
if node.right:
157+
parent_node = node
158+
node = node.right
159+
else:
160+
print('Value not found')
161+
break
162+
else:
163+
if node.left and node.right: # node with left and child
164+
node.value = get_min_node_value(node.right)
165+
node.right.remove(node.value, node)
166+
elif parent_node is None: # parent node
167+
if node.left:
168+
node.value = node.left.value
169+
node.right = node.left.right
170+
node.left = node.left.left
171+
elif node.right:
172+
node.value = node.right.value
173+
node.left = node.right.left
174+
node.right = node.right.right
175+
else: # parent node with no children
176+
node.value = None
177+
178+
elif parent_node.left == node: # found in the left node with right None
179+
parent_node.left = node.left if node.left else node.right
180+
elif parent_node.right == node: # found in the right node with left None
181+
parent_node.right = node.left if node.left else node.right
182+
break
183+
184+
return self
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
# Time O(n) | Space O(logn)
42
def maxPathSum(tree):
53
_, maxSum = findMaxSum(tree)
@@ -8,15 +6,13 @@ def maxPathSum(tree):
86
def findMaxSum(tree):
97
if tree is None:
108
return (0, 0)
11-
129
leftMaxSumAsBranch, leftMaxPathSum = findMaxSum(tree.left)
1310
rightMaxSumAsBranch, rightMaxPathSum = findMaxSum(tree.right)
1411
maxChildSumAsBranch = max(leftMaxSumAsBranch, rightMaxSumAsBranch)
1512

1613
value = tree.value
17-
maxSumAsBranch = max(maxChildSumAsBranch + value, value)
18-
# max path sum for triangle path which includes root as node
19-
maxSumAsRootNode = max(leftMaxSumAsBranch + value + rightMaxSumAsBranch, maxSumAsBranch)
20-
maxPathSum = max(leftMaxPathSum, rightMaxPathSum, maxSumAsRootNode)
14+
maxSumAsBrach = max(maxChildSumAsBranch + value, value)
15+
maxSumAsRootNoode = max(leftMaxSumAsBranch + value + rightMaxSumAsBranch, maxSumAsBrach)
16+
maxPathSum = max(leftMaxPathSum, rightMaxPathSum, maxSumAsRootNoode)
2117

22-
return (maxSumAsBranch, maxPathSum)
18+
return (maxSumAsBrach, maxPathSum)

algoexpert.io/python/Quick_Select.py

+23-31
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
2-
3-
4-
def quickSelect(array, k):
1+
def quickselect(array, k):
52
position = k - 1
6-
return quickSelectHelper(array, 0, len(array) - 1, position)
7-
8-
9-
def quickSelectHelper(array, startIndex, endIndex, position):
10-
while True:
11-
if startIndex > endIndex:
12-
raise Exception('Algorithm should never arrive here!')
13-
pivotIndex = startIndex
14-
leftIndex = startIndex + 1
15-
rightIndex = endIndex
16-
while leftIndex <= rightIndex:
17-
if array[leftIndex] > array[pivotIndex] and array[rightIndex] < array[pivotIndex]:
18-
swap(leftIndex, rightIndex, array)
19-
if array[leftIndex] <= array[pivotIndex]:
20-
leftIndex += 1
21-
if array[rightIndex] >= array[pivotIndex]:
22-
rightIndex -= 1
23-
swap(pivotIndex, rightIndex, array)
24-
if rightIndex == position:
25-
return array[rightIndex]
26-
elif rightIndex < position:
27-
startIndex = rightIndex + 1
28-
else:
29-
endIndex = rightIndex - 1
30-
3+
return quickSelectHelper(array, 0, len(array) - 1, position)
314

32-
def swap(one, two, array):
33-
array[one], array[two] = array[two], array[one]
5+
def quickSelectHelper(array, srtIdx, endIdx, position):
6+
while True:
7+
if srtIdx > endIdx:
8+
raise Exception("Algorrithm should never be here!")
9+
pivotIdx = srtIdx
10+
leftIdx = srtIdx + 1
11+
rightIdx = endIdx
12+
while leftIdx <= rightIdx:
13+
if array[leftIdx] > array[pivotIdx] > array[rightIdx]:
14+
array[leftIdx], array[rightIdx] = array[rightIdx], array[leftIdx]
15+
if array[leftIdx] <= array[pivotIdx]:
16+
leftIdx += 1
17+
if array[rightIdx] >= array[pivotIdx]:
18+
rightIdx -= 1
19+
array[pivotIdx], array[rightIdx] = array[rightIdx], array[pivotIdx]
20+
if rightIdx == position:
21+
return array[position]
22+
elif rightIdx > position:
23+
endIdx = rightIdx - 1
24+
else:
25+
srtIdx = rightIdx + 1

algoexpert.io/python/Zigzag_Traverse.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def zigzagTraverse(array):
77
result = []
88
row, col = 0, 0
99
goingDown = True
10-
while not isOutOfBounds(row, col, height, width):
10+
while not isOutOfBound(height, width, row, col):
1111
result.append(array[row][col])
1212
if goingDown:
1313
if col == 0 or row == height:
@@ -20,7 +20,7 @@ def zigzagTraverse(array):
2020
row += 1
2121
col -= 1
2222
else:
23-
if row == 0 or col == width:
23+
if col == width or row == 0:
2424
goingDown = True
2525
if col == width:
2626
row += 1
@@ -29,8 +29,9 @@ def zigzagTraverse(array):
2929
else:
3030
row -= 1
3131
col += 1
32+
3233
return result
3334

3435

35-
def isOutOfBounds(row, col, height, width):
36-
return row < 0 or row > height or col < 0 or col > width
36+
def isOutOfBound(height, width, row, col):
37+
return row < 0 or row > height or col < 0 or col > width
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://tinyurl.com/qmbd2vl
2+
class Solution(object):
3+
def findMaxLength(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: int
7+
"""
8+
count, maxLenSoFar = 0, 0
9+
counterMap = {count: -1} # count:index,
10+
11+
for i in range(len(nums)):
12+
currentNum = nums[i]
13+
if currentNum == 0:
14+
count -= 1
15+
else:
16+
count += 1
17+
18+
if count in counterMap:
19+
maxLenSoFar = max(maxLenSoFar, i - counterMap[count])
20+
else:
21+
counterMap[count] = i
22+
23+
return maxLenSoFar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Source: https://tinyurl.com/yad2akx6
2+
class Solution(object):
3+
def checkValidString(self, s):
4+
"""
5+
:type s: str
6+
:rtype: bool
7+
"""
8+
cmin, cmax = 0, 0 # openCount in range [cmin, cmax]
9+
for ch in s:
10+
if ch == "(":
11+
cmin += 1
12+
cmax += 1
13+
elif ch == ")":
14+
cmin -= 1
15+
cmax -= 1
16+
elif ch == "*":
17+
cmax += 1 # if `*` become `(` then openCount++
18+
cmin -= 1 # if `*` become `)` then openCount--
19+
# if `*` become `` then nothing happens
20+
# So openCount will be in new range [cmin-1, cmax+1]
21+
if cmax < 0:
22+
return False # Don't have enough openCount -> Invalid
23+
cmin = max(cmin, 0) # Keep openCount >= 0
24+
return cmin == 0 # Return true if can found `openCount == 0` in range [cmin, cmax]

0 commit comments

Comments
 (0)