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 W3_interview_k-diff-pairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def findPairs(self, nums: List[int], k: int) -> int:

seen = set()
result = set()

for i in range(0, len(nums)):
big = nums[i] + k
small = nums[i] - k

if big in seen:
res = tuple(sorted([nums[i], big]))
result.add(res)

if small in seen:
res1 = tuple(sorted([nums[i], small]))
result.add(res1)

seen.add(nums[i])

return len(result)

# Time complexity: O(n)
# Space complexity: O(n)
26 changes: 26 additions & 0 deletions W3_interview_pascals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def generate(self, numRows: int) -> List[List[int]]:

result = []

# Insert 0s for all the rows in in creasing order
for i in range(numRows):
row = [0] * (i + 1)
result.append(row)

# Insert 1s in first and last index of each row
for i in range(numRows):
result[i][0] = 1
result[i][-1] = 1

# Fill the middle values
for i in range(2, numRows):
for j in range(1, i):
result[i][j] = result[i - 1][j - 1] + result[i - 1][j]

return result

# Time complexity: O(numRows^2)
# Space complexity: O(numRows^2), because we store the full triangle