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
34 changes: 34 additions & 0 deletions k-diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Time Complexity : O(NlogN)
# 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:
# I am sorting the array and using two pointers to find the pairs with difference k.
# If the difference is less than k, I am moving the right pointer to increase the difference
# If the difference is more than k, I am moving the left pointer to decrease the difference
# If the difference is equal to k, I am adding the pair to a set to avoid duplicates and moving both pointers forward.
# In the end, I am returning the length of the set which contains the unique pairs.

from typing import List
class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
nums.sort()
seen = set()
i = 0
j = 1
while i < len(nums) and j < len(nums):
if i == j or abs(nums[i] - nums[j]) < k:
print("j",abs(nums[i] - nums[j]))
j += 1
elif abs(nums[i] - nums[j]) > k:
print("i",abs(nums[i] - nums[j]))
i += 1
else:
seen.add((nums[i] ,nums[j]))
i += 1
j += 1
print(seen)
return len(seen)

24 changes: 24 additions & 0 deletions pascal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Time Complexity : O(N^2)
# Space Complexity : O(N^2)
# 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:
# I am using a nested loop to generate the pascal triangle.
# The outer loop is for the number of rows and the inner loop is for the number of columns.
# I am initializing the first and last element of each row to 1.
# For the other elements, I am adding the two elements above it to get the current element.
# Finally, I am returning the list of lists which contains the pascal triangle.

from typing import List
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
ans = []
for i in range(numRows):
r = [1] * (i+1)
for j in range(1,i):
r[j] = ans[i-1][j-1]+ans[i-1][j]
ans.append(r)
return ans