diff --git a/k-diff.py b/k-diff.py new file mode 100644 index 00000000..0e2f8cdc --- /dev/null +++ b/k-diff.py @@ -0,0 +1,35 @@ +# The code defines a findPairs method to count the number of unique k-diff pairs in a list of integers. +# A k-diff pair is defined as two numbers in the list where the absolute difference between them is equal to k. + +# Initial Setup: +# - Use the Counter class to count the frequency of each number in nums and store it in 'count'. +# - This provides an efficient way to check for the existence of elements and their frequencies. + +# Case 1: k > 0 +# - For a positive difference k, iterate over each unique number in 'count'. +# - Check if the current number + k exists in the Counter. +# - This means there exists a pair (i, i + k) with the desired difference. +# - Sum up the results of these checks (True counts as 1) to get the total number of such pairs. + +# Case 2: k == 0 +# - For a zero difference, a valid pair exists only if the frequency of a number in the list is greater than 1. +# - Iterate over each number in 'count' and check if its frequency is greater than 1. +# - Sum up the results of these checks to get the count of such pairs. + +# Final Result: +# - The method returns the total number of unique k-diff pairs found in the list. + +# TC: O(n) - The time complexity is linear as the Counter is constructed in O(n), and each unique number in the Counter is processed once. +# SC: O(n) - The space complexity is linear due to the storage of the Counter, which stores the frequency of each unique number. + + +from typing import Counter, List + + +class Solution: + def findPairs(self, nums: List[int], k: int) -> int: + count = Counter(nums) + if k > 0: + return sum([i + k in count for i in count]) + else: + return sum([count[i] > 1 for i in count]) \ No newline at end of file diff --git a/pascal.py b/pascal.py new file mode 100644 index 00000000..08d45457 --- /dev/null +++ b/pascal.py @@ -0,0 +1,36 @@ +# The code defines a generate method to create Pascal's Triangle with the specified number of rows. +# Pascal's Triangle is a triangular array where each element is the sum of the two elements directly above it in the previous row. + +# Initialization: +# - 'triangle' is an empty list that will store the rows of Pascal's Triangle. + +# Main Loop: +# - Iterate over the range from 0 to numRows - 1 (inclusive), where each iteration corresponds to constructing one row of the triangle: +# - Create a new row filled with ones, with a length equal to the current row index + 1. +# - For rows with more than two elements (i.e., i > 0): +# - Calculate the inner elements of the row (from index 1 to i - 1) using values from the previous row. +# - Each inner element at index j is computed as the sum of triangle[i - 1][j - 1] (left parent) and triangle[i - 1][j] (right parent). +# - Append the completed row to 'triangle'. + +# Final Result: +# - After all rows are constructed, 'triangle' contains the complete Pascal's Triangle with numRows rows, which is returned. + +# TC: O(n^2) - Constructing each row involves iterating over the row's length, resulting in quadratic time complexity for n rows. +# SC: O(n^2) - The space complexity is proportional to the total number of elements in the triangle, which is the sum of the first n integers. + + +from typing import List + +class Solution: + def generate(self, numRows: int) -> List[List[int]]: + triangle = [] + + for i in range(numRows): + row = [1] * (i + 1) + + for j in range(1, i): + row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j] + + triangle.append(row) + + return triangle \ No newline at end of file