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 PascalTraingle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity :O(numRows^2)
// Space Complexity :O(1) if we don't consider the output list, otherwise O(numRows^2) for the output list
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No , a bit of confusion in the inner loop but I got it after dry run

// Your code here along with comments explaining your approach
//The first and last elements of every row are always 1
//Every middle element is the sum of the two elements directly above it from the previous row
//So, if we already know the previous row, we can build the current row easily by applying this rule.
//If it's the first or last position, add 1
//Otherwise, compute the value as:
//res[i-1][j-1] + res[i-1][j] (from the previous row)
import java.util.*;

class Solution {
public List<List<Integer>> generate(int numRows) {

List<List<Integer>> res = new ArrayList<>();

res.add(new ArrayList<>(Arrays.asList(1)));

for (int i = 1; i < numRows; i++) {

List<Integer> list = new ArrayList<>();

for (int j = 0; j <= i; j++) {

if (j == 0 || j == i) {
list.add(1);
} else {
list.add(res.get(i - 1).get(j - 1) + res.get(i - 1).get(j));
}
}

res.add(list);
}

return res;
}
}
43 changes: 43 additions & 0 deletions SumofK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time Complexity : o(n) where n is the number of elements in the input array
// Space Complexity : O(n) where n is the number of unique elements in the input array
// 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
// We can use a HashMap to store the frequency of each number in the input array as we need uniqy pairs and
// we can easily check if the pair exists in the map
// We can iterate through the map and check for each key if the pair (key + k) exists in the map
// If k is 0, we need to check if the frequency of the key is greater than 1 to count it as a valid pair,
// otherwise we can simply check if the pair exists
// We can keep a count of valid pairs and return it at the end
import java.util.*;

class Solution {
public int findPairs(int[] nums, int k) {

Map<Integer, Integer> map = new HashMap<>();
int count = 0;

// build frequency map
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}

// check pairs
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {

int key = entry.getKey();
int value = entry.getValue();

if (k == 0 && value > 1) {
count++;
}
else if (k != 0 && map.containsKey(key + k)) {
count++;
}
}

return count;
}
}