Skip to content
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
38 changes: 38 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#Pascal's Triangle
# Time Complexity :O(n2)
# 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
#start with first row of pascal triangle as [1] and then iterate through numsRows and keep adding arrays to result array by adding sum of the two numbers directly above and also parallely checking the two edges of each row
#time - O(n2)
#space - O(1)

class Solution:
def generate(self, numRows: int) -> List[List[int]]:
result = [[]]
if(numRows==0):
return result
result = [[1]]
i=2
while(i<=numRows):
p1=0
p2=1
curr=[]
j=0
while(j<i):
if(j==0 or j==i-1): #check if it is edges of each row
curr.append(1)
j=j+1
continue
curr.append(result[i-2][p1]+result[i-2][p2]) #sum of the two numbers directly above
j=j+1
p1=p1+1
p2=p2+1
result.append(curr)
i=i+1
return result


27 changes: 27 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#K-diff Pairs in an Array
# Time Complexity :O(n)
# Space Complexity :O(n)
# 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
#maintain a frequency map, iterate through hashmap and if incase the diff is zero increase count by 1 at each element
#check if key+k is present in map dna increase +1 as pair is found
# time - O(n)
# space - O(n)
class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
hashMap = Counter(nums) # frequency map
count=0
for key, value in hashMap.items():
if k==0:
if value>1:
count =count+1
continue
if key+k in hashMap:
count=count+1
return count