diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..7c2adfce --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,42 @@ + +// Time Complexity :O(n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this : a little near creating hashset logic + + +// Your code here along with comments explaining your approach +// Use a HashSet (prev_visited_elem) to track numbers seen so far so you can instantly check if a matching pair exists. +// For each number, compute x = num - k and y = num + k and check if either value was seen before — if yes, that forms a valid k-diff pair. +// Store each pair in a set (output) to avoid duplicates, and return the number of unique pairs found. + +class Solution { + public int findPairs(int[] nums, int k) { + if (k < 0) return 0; + + HashSet prev_visited_elem = new HashSet<>(); + HashSet output = new HashSet<>(); + + for (int num : nums) { + int x = num - k; + int y = num + k; + + if (prev_visited_elem.contains(x)) { + // store pair in normalized order (smaller first) + int a = x; + int b = num; + output.add(a + "," + b); + } + + if (prev_visited_elem.contains(y)) { + int a = num; + int b = y; + output.add(a + "," + b); + } + + prev_visited_elem.add(num); + } + + return output.size(); + } +} diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..409587e6 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,26 @@ +# Time Complexity :O(n) +# Space Complexity :O(n) +# Did this code successfully run on Leetcode :yes +# Any problem you faced while coding this : a little near creating hashset logic + + +# Your code here along with comments explaining your approach +# Use a HashSet (prev_visited_elem) to track numbers seen so far so you can instantly check if a matching pair exists. +# For each number, compute x = num - k and y = num + k and check if either value was seen before — if yes, that forms a valid k-diff pair. +# Store each pair in a set (output) to avoid duplicates, and return the number of unique pairs found. + +class Solution: + def findPairs(self, nums: List[int], k: int) -> int: + if k < 0: + return 0 + prev_visited_elem = set() + output = set() + for num in nums: + x = num - k + y = num + k + if x in prev_visited_elem: + output.add((x, num)) + if y in prev_visited_elem: + output.add((num, y)) + prev_visited_elem.add(num) + return len(output) diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..7122ff2a --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,52 @@ +// 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 +// Start with the first two base rows of Pascal’s Triangle: [1] and [1, 1]. +// For each new row, compute inner values by adding two adjacent numbers from the previous row. +// Append 1 at the start and end of each row to complete it, and repeat until all rows are built. + +class Solution { + public List> generate(int numRows) { + if (numRows == 1) { + List> result = new ArrayList<>(); + result.add(Arrays.asList(1)); + return result; + } + + if (numRows == 2) { + List> result = new ArrayList<>(); + result.add(Arrays.asList(1)); + result.add(Arrays.asList(1, 1)); + return result; + } + + List> output = new ArrayList<>(); + output.add(Arrays.asList(1)); + output.add(Arrays.asList(1, 1)); + + for (int i = 2; i < numRows; i++) { + List prev = output.get(output.size() - 1); + List newRow = buildRow(prev, i); + output.add(newRow); + } + + return output; + } + + // Helper method similar to your Python res_row() + private List buildRow(List prev, int i) { + List row = new ArrayList<>(); + row.add(1); + + for (int k = 1; k < i; k++) { + row.add(prev.get(k - 1) + prev.get(k)); + } + + row.add(1); + return row; + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..6fa0ab6a --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,32 @@ +# 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 +# Start with the first two base rows of Pascal’s Triangle: [1] and [1, 1]. +# For each new row, compute inner values by adding two adjacent numbers from the previous row. +# Append 1 at the start and end of each row to complete it, and repeat until all rows are built. + +class Solution: + def generate(self, numRows: int) -> List[List[int]]: + if numRows == 1: + return [[1]] + if numRows == 2: + return [[1],[1,1]] + output = [[1],[1,1]] + def res_row(prev,i): + result = [] + result.append(1) + for k in range(1,i): + result.append(prev[k-1]+prev[k]) + result.append(1) + return result + for i in range(2,numRows): + prev= output[-1] + numrow_result = res_row(prev,i) + output.append(numrow_result) + return output + + \ No newline at end of file