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
40 changes: 40 additions & 0 deletions K_Diff_Pairs_Array.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer, Integer> 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;
}
}
38 changes: 38 additions & 0 deletions Pascals_Triangle.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> generate(int numRows) {
// Create output List of Lists
List<List<Integer>> result = new ArrayList<>();

// 'i' pointer - for each row
for(int i = 0; i < numRows; i++){
// Create a list for each row
List<Integer> 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;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.