forked from super30admin/Competitive_Coding-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Go through numbers, when a number is not seen before, add it to set (this way we will avoid duplicate pairs in future) | ||
* Check if number +k -k exist in set, if yes, update count. Handle k = 0 separately. Since for k = 0, we can have pair like 5, 5 | ||
* We use another hashset to store duplicate seen. duplicate pair will be counted only once hence use another set | ||
* | ||
* Time Complexity O(N) N: size of nums | ||
* Space Complexity: O(N + N) worst case, all numbers will be stored in both sets | ||
*/ | ||
public class KDiffPairs_LC532 { | ||
public int findPairs(int[] nums, int k) { | ||
int count = 0; // Output variable | ||
|
||
if (nums == null || nums.length == 0 || k < 0) // Edge inputs | ||
return count; | ||
|
||
Set<Integer> set = new HashSet<>(); | ||
Set<Integer> duplicates = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
if (!set.contains(num)) { // If number is not seen before | ||
set.add(num); // Add it to set | ||
if (k == 0) // When k is 0, don't check for further conditions | ||
continue; | ||
if (set.contains(num - k)) // Pair found | ||
count += 1; | ||
if (set.contains(num + k)) // Pair found | ||
count += 1; | ||
} else if (k == 0 && !duplicates.contains(num)) { // When k is 0, i & j can be same numbers | ||
duplicates.add(num); // Add number to duplicates set when seen twice | ||
count += 1; // Same number allowed only once, so use duplicate set | ||
} | ||
} | ||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PascalTriangleII_LC119 { | ||
|
||
/** | ||
* First pascal row is always [1]. Keep it hardcoded. Loop for 1 to rowIndex, use row's prev elements to find | ||
* intermediate elements in current row. Add 1 in the end for a row | ||
* | ||
* Time Complexity O(N^2) where n is number of rows | ||
* Space Complexity: O(N) only one list of size n is used | ||
* | ||
*/ | ||
public List<Integer> getRow(int rowIndex) { | ||
List<Integer> row = new ArrayList<>(); // output row | ||
|
||
if (rowIndex < 0) // Handle invalid input | ||
return row; | ||
|
||
row.add(1); // First element is always 1 | ||
|
||
for (int i = 1; i < rowIndex + 1; i++) { // Calculate each row upto rowIndex | ||
for (int j = i - 1; j > 0; j--) { // Middle elements computation using row's prev values | ||
row.set(j, row.get(j - 1) + row.get(j)); | ||
} | ||
row.add(1); // Add last element 1 | ||
} | ||
return row; | ||
} | ||
} |