Skip to content

Commit fdf2e81

Browse files
author
Partho Biswas
committed
Leetcode - Google Phone Mock 5
1 parent 8fe939f commit fdf2e81

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

leetcode.com/python/222_Count_Complete_Tree_Nodes.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# self.right = None
77

88
class Solution(object):
9-
109
def countDepth(self, node):
1110
depth = 0
1211
while node.left:
@@ -45,3 +44,17 @@ def countNodes(self, root):
4544
right = pivot - 1
4645
return (2 ** depth - 1) + left
4746

47+
48+
# My solution during Leetcode Mock Interview
49+
class Solution(object):
50+
def countNodes(self, root):
51+
"""
52+
:type root: TreeNode
53+
:rtype: int
54+
"""
55+
if not root:
56+
return 0
57+
leftCount = self.countNodes(root.left)
58+
rightCount = self.countNodes(root.right)
59+
totalCount = leftCount + 1 + rightCount
60+
return totalCount

leetcode.com/python/562_Longest_Line_of_Consecutive_One_in_Matrix.py

+27
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,30 @@ def longestLine(self, M):
136136
maxOnes = max([maxOnes, dp[rowIdx][colIdx][0], dp[rowIdx][colIdx][1], dp[rowIdx][colIdx][2], dp[rowIdx][colIdx][3]])
137137

138138
return maxOnes
139+
140+
141+
# My solutions during Leetcode Mock Interview
142+
class Solution(object):
143+
def longestLine(self, M):
144+
"""
145+
:type M: List[List[int]]
146+
:rtype: int
147+
"""
148+
if not M:
149+
return 0
150+
151+
dp = [[[0, 0, 0, 0] for _ in range(len(M[0]) + 2)] for _ in range(len(M) + 1)]
152+
maxLen = 0
153+
for i in range(1, len(M) + 1):
154+
for j in range(1, len(M[0]) + 1):
155+
if M[i - 1][j - 1] == 1:
156+
ho = dp[i][j - 1][0] + 1
157+
vt = dp[i - 1][j][1] + 1
158+
di = dp[i - 1][j - 1][2] + 1
159+
ad = dp[i - 1][j + 1][3] + 1
160+
lineLengths = [ho, vt, di, ad]
161+
dp[i][j] = lineLengths
162+
currentMaxLen = max(lineLengths)
163+
maxLen = max(maxLen, currentMaxLen)
164+
# print("DP: ", dp)
165+
return maxLen

leetcode.com/python/844_Backspace_String_Compare.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# O(N + N) time | # O(N + N) space
32
class Solution(object):
43
def backspaceCompare(self, S, T):
@@ -20,10 +19,6 @@ def getOriginalString(self, givenStr):
2019
return "".join(originalStr)
2120

2221

23-
24-
25-
26-
2722
# O(N + N) time | # O(1) space -- Two Pointer solution
2823
class Solution(object):
2924
def backspaceCompare(self, S, T):
@@ -62,3 +57,28 @@ def getNextValidCharIndex(self, givenStr, givenIndex):
6257

6358

6459

60+
# My solution during Leetcode Mock Interview
61+
class Solution(object):
62+
def backspaceCompare(self, S, T):
63+
"""
64+
:type S: str
65+
:type T: str
66+
:rtype: bool
67+
"""
68+
sV = []
69+
for sc in S:
70+
if sc == "#":
71+
if sV and len(sV) > 0:
72+
sV.pop()
73+
else:
74+
sV.append(sc)
75+
76+
tV = []
77+
for tc in T:
78+
if tc == "#":
79+
if tV and len(tV) > 0:
80+
tV.pop()
81+
else:
82+
tV.append(tc)
83+
84+
return "".join(sV) == "".join(tV)

0 commit comments

Comments
 (0)