├── Binary_Tree_Inorder_Traversal.py ├── Climb_Stairs.py ├── Longest_Common_Prefix.md ├── Maximum_Depth_of_Binary_Tree.py ├── Number_of_1_Bits.py ├── PalindromNumber.md ├── Remove_Duplicate.md ├── Reverse_Bits.py ├── TwoSum.md └── Valid_Paranatheses.md /Binary_Tree_Inorder_Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: 9 | if root is None: 10 | return [] 11 | return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) -------------------------------------------------------------------------------- /Climb_Stairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def climbStairs(self, n: int) -> int: 3 | if n <= 1: 4 | return 1 5 | array = [0] * (n + 1) 6 | array[0] = 1 7 | array[1] = 1 8 | 9 | for i in range(2, n + 1): 10 | array[i] = array[i - 1] + array[i - 2] 11 | 12 | return array[n] -------------------------------------------------------------------------------- /Longest_Common_Prefix.md: -------------------------------------------------------------------------------- 1 | 2 | # Longest Common Prefix 3 | Longest Common Prefix is a problem where you Write a function to find the longest common prefix string amongst an array of strings. 4 | 5 | ## how did i solved it 6 | i created a variable s that hold's the first string of the array then i looped throw the whole array , as i did that i checked the same first letters between s and the word and assigned that to s 7 | 8 | ## Complexity 9 | - Time complexity: 10 | O(n) // 41ms 11 | 12 | - Space complexity: 13 | 16mb 14 | 15 | ## Code 16 | ``` 17 | class Solution: 18 | def longest_common_prefix(self , word1, word2): 19 | prefix = [] 20 | for char1, char2 in zip(word1, word2): 21 | if char1 == char2: 22 | prefix.append(char1) 23 | else: 24 | break 25 | return ''.join(prefix) 26 | 27 | def longestCommonPrefix(self, strs) -> str: 28 | s = strs[0] 29 | for word in strs[1:] : 30 | s = self.longest_common_prefix(s, word) 31 | return s 32 | ``` -------------------------------------------------------------------------------- /Maximum_Depth_of_Binary_Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def maxDepth(self, root: Optional[TreeNode]) -> int: 9 | if root is None : return 0 10 | return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)); -------------------------------------------------------------------------------- /Number_of_1_Bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n: int) -> int: 3 | return bin(n).count('1') -------------------------------------------------------------------------------- /PalindromNumber.md: -------------------------------------------------------------------------------- 1 | 2 | # Palindrom number 3 | palindrom number is a problem where you check if a number in reverse is the same as its original state 4 | 5 | ## how did i solved it 6 | this one is pretty sample easy instead of dealing with numbers we deal with them as string 7 | now it become pretty simple we just reverse the string and check if its equal to its no reversed state 8 | 9 | ## Complexity 10 | - Time complexity: 11 | O(n) // 45ms 12 | 13 | - Space complexity: 14 | 17mb 15 | 16 | ## Code 17 | ``` 18 | class Solution: 19 | def isPalindrome(self, x: int) -> bool: 20 | return str(x)[::-1] == str(x) 21 | ``` -------------------------------------------------------------------------------- /Remove_Duplicate.md: -------------------------------------------------------------------------------- 1 | 2 | # Remove_Duplicates 3 | Remove_Duplicate is a problem where you are Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. 4 | ## how did i solved it 5 | 6 | The variable j is used to keep track of the current index where a unique element should be placed and the list is sorted and thats helpfull alot , so we loop throw it , if we found two diffrent near by numbers that mean's we found a unique number we place it in the j index at increment j 7 | 8 | ## Complexity 9 | - Time complexity: 10 | O(n) // 62ms 11 | 12 | - Space complexity: 13 | 18mb 14 | 15 | ## Code 16 | ``` 17 | class Solution: 18 | def removeDuplicates(self, nums: List[int]) -> int: 19 | j = 1 20 | for i in range(1, len(nums)): 21 | if nums[i] != nums[i - 1]: 22 | nums[j] = nums[i] 23 | j += 1 24 | return j 25 | ``` -------------------------------------------------------------------------------- /Reverse_Bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseBits(self, n: int) -> int: 3 | return int(bin(n)[2:].zfill(32)[::-1], 2) -------------------------------------------------------------------------------- /TwoSum.md: -------------------------------------------------------------------------------- 1 | 2 | # TwoSum 3 | TwoSum is a problem where it required of you to find two numbers in a given array that sum's up to a defined target and return their index's in that array 4 | 5 | ## how did i solved it 6 | as i start i tried to find a way to store a number and then see if it adds up with another number so i had the idea of a hashmap or a 'dict' so i made a dict that store all numbers that we have passed on , 7 | but how do we find the number that adds up with one of the numbers stored in the dict , 8 | we simply loop throw the whole array or list of numbers , in case the number we are at adds up with one of the numbers stored in the dict we return them , else we store that number in the dict and loop again 9 | 10 | in short terms we add a number in the dict and search for another number that if we add them togather would sum up to tha target and we know if they adds up simply by checking if target - nums[i] is in that dict that means that dict[number] + nums[i] = target 11 | 12 | ## Complexity 13 | - Time complexity: 14 | O(n) // 55ms 15 | 16 | - Space complexity: 17 | 17mb 18 | 19 | ## Code 20 | ``` 21 | class Solution: 22 | def twoSum(self, nums: List[int], target: int) -> List[int]: 23 | storedNumbers = {} 24 | for i in range(len(nums)) : 25 | NumberToAdd = target - nums[i] 26 | if NumberToAdd in storedNumbers : 27 | return i , storedNumbers[NumberToAdd] 28 | storedNumbers[nums[i]] = i 29 | ``` 30 | -------------------------------------------------------------------------------- /Valid_Paranatheses.md: -------------------------------------------------------------------------------- 1 | 2 | # Valid Parantheses 3 | 4 | Valid Parantheses is a leet code problem where you are Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 5 | 6 | An input string is valid if: 7 | 8 | -Open brackets must be closed by the same type of brackets. 9 | -Open brackets must be closed in the correct order. 10 | -Every close bracket has a corresponding open bracket of the same type. 11 | 12 | ## how did i solved it 13 | this one a lil tricky , i used a hashmap to link each opening with its closing , and as i maped throw the string i appended every opening , while i was checking if i reached a closing , when we reach a closing the last elemnent in the array must be its openning , 14 | the tricky thing here is that you must return True only if the array is empty at the end of the operation 15 | 16 | ## Complexity 17 | - Time complexity: 18 | O(n) // 34ms 19 | 20 | - Space complexity: 21 | 16mb 22 | 23 | ## Code 24 | ``` 25 | def isValid(self, s: str) -> bool: 26 | l = [] 27 | hashMap = { 28 | '(' : ')', 29 | '{' : '}', 30 | '[' : ']' 31 | } 32 | for i in s : 33 | if i in "({[" : 34 | l.append(i) 35 | elif l and i in ")}]" and hashMap[l[-1]] == i : 36 | l.pop() 37 | else : 38 | return False 39 | 40 | return True if not l else False 41 | ``` --------------------------------------------------------------------------------