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
45 changes: 45 additions & 0 deletions leetcode_118.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 14:16:29 2026

@author: rishigoswamy

LeetCode 118: Pascal's Triangle
Link: https://leetcode.com/problems/pascals-triangle/

Approach:
Build the triangle row by row.

Observations:
- First and last elements of every row are 1.
- For any middle element:
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]

Steps:
1. Iterate from row 0 to numRows-1.
2. Initialize each row with all 1s.
3. Fill middle values using previous row.
4. Append row to result.

// Time Complexity : O(n^2)
Total elements generated ≈ n(n+1)/2.
// Space Complexity : O(n^2)
Output storage for triangle.
"""

from typing import List

class Solution:
def generate(self, numRows: int) -> List[List[int]]:
res = []

for i in range(0, numRows):
temp = [1] * (i+1)

for j in range(1, i):
temp[j] = res[i-1][j-1] + res[i-1][j]

res.append(temp)

return res
52 changes: 52 additions & 0 deletions leetcode_532.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 22 14:18:19 2026

@author: rishigoswamy

LeetCode 532: K-diff Pairs in an Array
Link: https://leetcode.com/problems/k-diff-pairs-in-an-array/

Approach:
Use a frequency hashmap.

Case 1: k == 0
We are looking for numbers that appear more than once.
Each number with frequency > 1 contributes exactly one pair.

Case 2: k > 0
For each unique number x,
check whether (x + k) exists in the map.

Important:
We use unique keys to avoid duplicate pair counting.

// Time Complexity : O(n)
Build hashmap in O(n)
Iterate unique keys in O(n)
// Space Complexity : O(n)
Hashmap storage
"""

from typing import List
from collections import defaultdict

class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
hMap = defaultdict(int)
for num in nums:
hMap[num] += 1

count = 0
if k == 0:
for key in hMap:
if hMap[key] > 1:
count+=1
return count

for key in hMap:
if k + key in hMap:
count+=1

return count