diff --git a/k-diff.py b/k-diff.py new file mode 100644 index 00000000..627a60cb --- /dev/null +++ b/k-diff.py @@ -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) + \ No newline at end of file diff --git a/pascal.py b/pascal.py new file mode 100644 index 00000000..b1a3f8cf --- /dev/null +++ b/pascal.py @@ -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 + \ No newline at end of file