Code
Today I solved the below problems:
1. Problem Statement —Sum Root to Leaf Numbers
Solution:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
self.ans = 0
def dfs(root,depth):
if root:
if root.left is None and root.right is None:
val = depth * 10 + root.val
self.ans += val
dfs(root.left,depth * 10 + root.val)
dfs(root.right,depth * 10 + root.val)
dfs(root,0)
return self.ans
Analysis:
Time Complexity : O(N)
Space Complexity : O(1)
2. Problem Statement — Palindrome Partitioning
Solution:
class Solution:
def partition(self, s: str) -> List[List[str]]:
lst = []
def palindrome(a):
return a == a[::-1]
def dfs(i,curr):
if i == len(s):
lst.append(curr)
return
for j in range(i,len(s)):
sol = s[i:j+1]
if palindrome(sol):
dfs(j+1, curr + [s[i:j+1]])
return
dfs(0,[])
return lst
Analysis:
Time Complexity : O(N)
Space Complexity : O(1)
Courses
- Data Analysis with Python — I continued learning about data analysis in python using pandas library.
Research/ Projects
I learned more about Spacy for date extraction.
Novels
Today, I found a quote I loved:
"The most important law of human nature is that we are highly adaptable creatures. We can adjust to any circumstance or environment."- Robert Greene
That's it for today. May we meet again, and until then take care.
Let's connect on LinkedIn🙋♂️