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 #957

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
66 changes: 66 additions & 0 deletions KDiffPairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

// Using a hashmap to store all the values as key and their frequency as the value. Then iterating through hashmap, for each key,
// check if nums[i]-k==key and the key is not negative indicating that this is the first time we are visiting this key, increment
// the counter. Handle differently for k=0, check if any key in hashmap is having value >=2, that means that is pair that will give
// difference as 0, so increment counter. Return counter

// Time Complexity : O(n^2)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no
import java.util.*;

class Solution {
public int findPairs(int[] nums, int k) {
// Declaring hashmap
HashMap<Integer, Integer> map = new HashMap<>();
// Iterate through nums and add all the values as keys in hashmap and there
// frequency as value
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);

}
// System.out.println(map);
// Declaring result count
int cnt = 0;
// Handle for k==0 case
if (k == 0) {
// Iterate through hashmap
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
// check any value greater than or equal 2 that means that element is present
// more than one times in nums
if (value >= 2) {
// Increment count to indicate that pair found
cnt++;
}
}
}
// Handle for other case
else {
// Iterate through hashmap
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
// Iterate through each value in nums
for (int i = 0; i < nums.length; i++) {
// check if key==nums[i]-k
if (key == nums[i] - k) {
// Then check if the key is not -1, indicating this is first visit to this key
if (map.get(key) != -1) {
// In that case, increment count
cnt++;
// And modify the value to -1, which will indicate already visited
map.put(key, -1);
}

}

}
}
}
// Return count of pairs
return cnt;
}
}
44 changes: 44 additions & 0 deletions PascalTraingle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// Running 2 loops, for each row value is the sum of values in previous row, same column and previous row, column-1. For all rows,
// first and last element will be same i.e. 1, so we can add it directly.

// Time Complexity : O(n^2)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

import java.util.*;

class Solution {
public List<List<Integer>> generate(int numRows) {
// Since they are expecting list<list<Integer>> as result, declare it to store
// output
List<List<Integer>> result = new ArrayList<>();
// First row will be same having value 1, so declare it
List<Integer> firstRow = new ArrayList<>();
// Add 1
firstRow.add(1);
// Add first row to result
result.add(firstRow);
// Start a loop from 1 to numRows
for (int i = 1; i < numRows; i++) {
// Get the previous row
List<Integer> prev = result.get(i - 1);
// Declare new arraylist for current row
List<Integer> curr = new ArrayList<>();
// Add first value as 1 to it
curr.add(1);
// Run a inner loop for current row
for (int j = 1; j < i; j++) {
// Add the sum of prev row, same col and prev row, col-1
curr.add(prev.get(j) + prev.get(j - 1));
}
// Add 1 for the last value
curr.add(1);
// Add current row to the result
result.add(curr);
}
// Return result containing all the rows
return result;
}
}