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
24 changes: 24 additions & 0 deletions k-diff_pairs_in_an_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#Time Complexity- O(n) Space Complexity- O(n)
class Solution:
def findPairs(self, nums: list[int], k: int) -> int:
s = set()
map = { }
for i in range(len(nums)):
map[nums[i]] = i
for i in range(len(nums)):
y = nums[i] - k

if y in map and map[y] != i:
if y > nums[i]:
s.add((y, nums[i]))
else:
s.add((nums[i],y))
y = nums[i] + k

if y in map and map[y] != i:
if y > nums[i]:
s.add((y, nums[i]))
else:
s.add((nums[i], y))

return len(s)
22 changes: 22 additions & 0 deletions pascals_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://leetcode.com/problems/pascals-triangle/description/

class Solution:
def generate(self, numRows: int) -> list[list[int]]:
# creating the first of the triangle which is always 1. It's list inside a list
first_row = [[1]]
# as first row is already made above so we need to made remaining rows
for i in range(numRows -1):
# creating new temp list
# we are taking 0 for our understanding to build the next row
temp = [0] + first_row[-1] + [0]
# creating an empty row for the next for loop
row = []
# building the next row and it's length would be the previous row+1
for j in range(len(first_row[-1]) + 1):
# apending the row
row.append(temp[j] + temp[j + 1])
first_row.append(row)
return(first_row)

solution = Solution()
print(solution.generate(5))