Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Competitive-Coding-3 Problems #941

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
27 changes: 27 additions & 0 deletions 2024_Problem41_Pascals_Triangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//118. Pascal's Triangle - https://leetcode.com/problems/pascals-triangle/description/
//Time Complexity: O(numsRows^2) - because we are generating all the elements of the Pascal's Triangle
//Space Complexity: O(numRows) - auxillary data structure is used to store 1 row at a time

class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
if(numRows == 0){
return result;
}
result.add(new ArrayList<>());
result.get(0).add(1); //first element of the Pascal's Triangle

for(int i=1; i<numRows; i++){
List<Integer> currRow = new ArrayList<>();
currRow.add(1); //first element of the currentRow (left-most element)
List<Integer> prevRow = result.get(i-1);
for(int j=1; j<i; j++){
//next row middle element are the sum of prev row elements
currRow.add(prevRow.get(j-1) + prevRow.get(j));
}
currRow.add(1); //adding right most element as 1 in currentRow
result.add(currRow);
}
return result;
}
}
19 changes: 19 additions & 0 deletions 2024_Problem42_K-diff_Pairs_in_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//532. K-diff Pairs in an Array - https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
//Time Complexity: O(n)
//Space Complexity: O(n)

class Solution {
public int findPairs(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for(int num : nums){
map.put(num, map.getOrDefault(num,0)+1);
}
int result = 0;
for(int i : map.keySet()){
if(k>0 && map.containsKey(i+k) || k==0 && map.get(i) > 1){
result++;
}
}
return result;
}
}