Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Competitive Coding Week 3 Completed #949

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions balancedBT.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions kDiffPairs.py
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions palindromeLL.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions pascalTriangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# // Time Complexity :O(n^2) for and while
# // 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


# // 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