Thursday 27 October 2022

Trees- 104. Maximum Depth of Binary Tree

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================

# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        else:
            leftDepth = self.maxDepth(root.left)
            rightDepth = self.maxDepth(root.right)
            
            if leftDepth > rightDepth:
                return leftDepth + 1
            else:
                return rightDepth + 1

Sunday 23 October 2022

Sorting and Searching - 278. First Bad Version

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================

# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:

class Solution:
    def firstBadVersion(self, n: int) -> int:
        low = 1
        high = n
        
        while low < high:
            mid = int((low+high)/2) 
            
            if isBadVersion(mid):
                high = mid
            else: 
                low = mid + 1
        return low
            
 
        

Others - 191. Number of 1 Bits

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================
class Solution:
    def hammingWeight(self, n: int) -> int:
        return bin(n).count('1')

Linked List - 237. Delete Node in a Linked List

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next

Thursday 20 October 2022

Sorting and Searching - 88. Merge Sorted Array

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        
        for i in range(n):
            nums1[i + m] = nums2[i]
        nums1.sort()

String - 14. Longest Common Prefix

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        min_elem = strs[0]

        for i in range(len(strs)):
            if len(min_elem) > len(strs[i]):
                min_elem = strs[i]

        #print(min_elem)

        for i in range(len(min_elem)):
            for j in range(len(strs)):
                if strs[j][i] != min_elem[i]:
                    return min_elem[:i]
            
        return min_elem

Wednesday 19 October 2022

String - 28. Implement strStr()

#===================================
# Tanzila Islam
# Email: tanzilamohita@gmail.com
# Language: Python 3
#===================================

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        h = len(haystack)
        n = len(needle)

        for i in range(h):
            if haystack[i:i+n]==needle:
                return i
        
        return -1