diff --git a/K_Diff_Pairs_Array.java b/K_Diff_Pairs_Array.java new file mode 100644 index 00000000..b7a1fc24 --- /dev/null +++ b/K_Diff_Pairs_Array.java @@ -0,0 +1,40 @@ +// Time Complexity : O(N) + O(N) = O(2N) = O(N) -> O(N) iterations to fill the map and O(N) to iterate through the map keys +// Space Complexity : O(N) -> To store 'N' elements in a map +// 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 +class Solution { + public int findPairs(int[] nums, int k) { + // Base condition + if(nums == null ||nums.length == 0 || k < 0){ + return 0; + } + + // 1. Initialize the counter to keep track of unique pairs + int pairs = 0; + + // 2. Create a HashMap to store frequenxy of each element in an array + HashMap counter = new HashMap<>(); + + // 3. 1st pass: Traverse array and fill the Map + for(int n : nums){ + counter.put(n, counter.getOrDefault(n, 0) + 1); // key: value, value = prev value count(0 or n) + 1 + } + + // 4. 2nd pass: Iterate over key-value pairs in the map using entrySet() + for(Map.Entry entry: counter.entrySet()){ + int numKey = entry.getKey(); // Get the key + int val = entry.getValue(); // Get the value + // If pair has distinct numbers and compliment of numKey is presen tin HashMap, increment pairs count. (numKey + k) to find compliment helps avoiding duplicate pairs ((1,4) and (4,1)) + if(k > 0 && counter.containsKey(numKey + k)){ + pairs++; + // If pair has same number i.e k=0 then their is more than one occurence of that number(val > 1) + }else if(k == 0 && val > 1){ + pairs++; + } + } + return pairs; + } +} \ No newline at end of file diff --git a/Pascals_Triangle.java b/Pascals_Triangle.java new file mode 100644 index 00000000..f065682f --- /dev/null +++ b/Pascals_Triangle.java @@ -0,0 +1,38 @@ +// Time Complexity : O(numRows^2) -> Two for loops each running for 'numRows=5' iterations +// Space Complexity : O(1) -> Output list created is returned as an output so no extra space used +// 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 +class Solution { + public List> generate(int numRows) { + // Create output List of Lists + List> result = new ArrayList<>(); + + // 'i' pointer - for each row + for(int i = 0; i < numRows; i++){ + // Create a list for each row + List row = new ArrayList<>(); + // 'j' pointer to fill all the columns in each row + for(int j = 0; j <= i; j++){ + // first and last columns in each row will be filled with 1 + if(j == 0 || j == i){ + row.add(1); + } + // All middle columns will be sum of two numbers directly above it in the previous row + else{ + int prev = result.get(i-1).get(j-1); + int next = result.get(i-1).get(j); + // Get both numbers and add sum to the row for that column + row.add(prev+next); + } + + } + // Add each row(list 'row') to the result list + result.add(row); + } + // Return list of lists + return result; + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f5c45b5f..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach \ No newline at end of file