From 5b8657fbb04c49f63cb464639febbe322201b7b9 Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Fri, 22 Nov 2024 13:45:50 -0600 Subject: [PATCH 1/3] 2/2 Completed Week3 --- balancedBT.py | 29 +++++++++++++++++++++++++++++ palindromeLL.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 balancedBT.py create mode 100644 palindromeLL.py diff --git a/balancedBT.py b/balancedBT.py new file mode 100644 index 00000000..6c3e4837 --- /dev/null +++ b/balancedBT.py @@ -0,0 +1,29 @@ +# // Time Complexity :O(n) +# // Space Complexity :O(h) +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this :No + + +# // Your code here along with comments explaining your approach + +class Solution: + def isBalanced(self, root: Optional[TreeNode]) -> bool: + # if not root: + # return True + return self.height(root) != -1 #check height at every step + + def height(self,root): + + if not root: # height is 0: at the lowest leaf + return 0 + + left = self.height(root.left) # left height + right = self.height(root.right) # right height + + if abs(left - right) >1: # left and right not equal + return -1 + + if left == -1 or right == -1: # even 1 subtree is not balanced + return -1 + + return max(left,right) + 1 #new height = maximum height(l,r) +1 diff --git a/palindromeLL.py b/palindromeLL.py new file mode 100644 index 00000000..60af6de8 --- /dev/null +++ b/palindromeLL.py @@ -0,0 +1,40 @@ +# // Time Complexity :O(n)-> n/2 for mid +n/2 for reverse + n for compare +# // Space Complexity :O(h) for reverse +# // Did this code successfully run on Leetcode :Yes +# // Any problem you faced while coding this :If you take slow.next instead of slow, you lose midpoint. + + +# // Your code here along with comments explaining your approach +class Solution: + def isPalindrome(self, head: Optional[ListNode]) -> bool: + if head is None : # Empty list + return True + + #Find mid point + slow = head + fast = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + #//dont break here This step creates error with reverse function slow.next = None + head2 = self.reverse(slow) # slow is on mid point, reverse it(dont lose the midpoint) + + # Compare the 2 Linked lists + while head and head2: + if head.val != head2.val: + return False + head = head.next + head2 = head2.next + return True + + # Reverse the linked list + def reverse(self,head): + prev = None + curr = head + while curr: + temp = curr.next + curr.next = prev + prev = curr + curr = temp + return prev \ No newline at end of file From c8eef1a3159167ee0f865972b63c0f8c6186d0a6 Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Fri, 22 Nov 2024 14:57:45 -0600 Subject: [PATCH 2/3] 3/4 Week3 --- pascalTriangle.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 pascalTriangle.py diff --git a/pascalTriangle.py b/pascalTriangle.py new file mode 100644 index 00000000..40f3933c --- /dev/null +++ b/pascalTriangle.py @@ -0,0 +1,25 @@ +# // Time Complexity :O(n^2) for and while +# // Space Complexity :O(1) +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this :No + + +# // Your code here along with comments explaining your approach + +class Solution: + def generate(self, numRows: int) -> List[List[int]]: + a = 1 + res = [[1]] + for i in range(2,numRows+1): #[[1],[1,0,1],[1,0,0,1]] + arr = [0] * i + arr[0] = 1 #first and last are 1 + arr[-1] = 1 + + # 2 pointers on previous result + l,h = 0,1 + while h < len(arr)-1: # height crosses new array length + arr[h] = res[-1][l] + res[-1][h] # add adjacent using old res + l+=1 + h+=1 + res.append(arr) # no need for deep copy + return res \ No newline at end of file From 07d35034b1bb203485d642099fd8c81e06a3eb85 Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Fri, 22 Nov 2024 15:59:17 -0600 Subject: [PATCH 3/3] 4/4 Week3 done --- kDiffPairs.py | 27 +++++++++++++++++++++++++++ pascalTriangle.py | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 kDiffPairs.py diff --git a/kDiffPairs.py b/kDiffPairs.py new file mode 100644 index 00000000..808390fc --- /dev/null +++ b/kDiffPairs.py @@ -0,0 +1,27 @@ +# // Time Complexity :O(n) for freqmap and checking +# // Space Complexity :O(n) for hashmap +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this : No + + +# // Your code here along with comments explaining your approach + +class Solution: + def findPairs(self, nums: list[int], k: int) -> int: + fmap = {} + counter = 0 + for num in nums: # frequencymap for special case + if num in fmap: + fmap[num] += 1 + else: + fmap[num] = 1 + + for key in fmap: # traverse for pairs + if k == 0: # special case for 0, 3-3-3-3 only two 3's are considered + if fmap[key] >1: + counter +=1 + else: + if key+k in fmap: # diff = 3; num = 1 , is 1+3 in map + counter +=1 + + return counter diff --git a/pascalTriangle.py b/pascalTriangle.py index 40f3933c..d74b1ac7 100644 --- a/pascalTriangle.py +++ b/pascalTriangle.py @@ -1,5 +1,5 @@ # // Time Complexity :O(n^2) for and while -# // Space Complexity :O(1) +# // Space Complexity :O(n^2) for arr*i // not O(1) # // Did this code successfully run on Leetcode : Yes # // Any problem you faced while coding this :No