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

mock 3 solutions in Java #972

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
54 changes: 54 additions & 0 deletions KDiffPairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Approach: Create a map with the unique elements of the array as keys and their corresponding frequencies as values. Iterate through the
// key set and search for (key + k) in the map, incrementing the count for each successful search. If k is 0, the frequency of the key must
// be greater than 1 to avoid counting the same number as a pair with itself.
// Time Complexity: O(n)
// Space Complexity: O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

import java.util.Map;
import java.util.Set;
import java.util.List;

import java.util.HashMap;
import java.util.HashSet;

public class KDiffPairs {

int countBF(int[] arr, int k) {
Set<List<Integer>> set = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (Math.abs(arr[i] - arr[j]) == k) {
set.add(List.of(Math.min(arr[i], arr[j]), Math.max(arr[i], arr[j])));
}
}
}
return set.size();
}

int count(int[] arr, int k) {
if (arr == null || arr.length == 0) {
return 0;
}
// integer to it's frequency map
Map<Integer, Integer> map = new HashMap<>();
for (int i: arr) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
int count = 0;
for (int key : map.keySet()) {
if (k != 0 && map.containsKey(key + k) || k == 0 && map.get(key) > 1) {
count++;
}
}
return count;
}

public static void main(String[] args) {
KDiffPairs kdp = new KDiffPairs();
int[] arr = { 5, 4, 2, 3, 3, 1, 1 };
int k = 2;
System.out.println("Number of unique pairs with difference " + k + " are: " + kdp.count(arr, k));
}
}
53 changes: 53 additions & 0 deletions PascalTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Approach: Create a matrix with given no.of rows (n) and nth odd number of columns. Initialize the first row with all 0s, except for the
// middle element, which should be set to 1. Then, fill the remaining matrix elements, where each element is computed as the sum of the
// elements immediately diagonally above the current element. Finally, print the matrix without the 0s.
// Time Complexity: O(n^2)
// Space Complexity: O(n^2)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

import java.util.Arrays;

public class PascalTriangle {

void print(int rows) {
// find nth odd number to determine # cols
int cols = 2 * rows - 1;
int[][] matrix = new int[rows][cols];
Arrays.fill(matrix[0], 0);
matrix[0][cols / 2] = 1;

for (int i = 1; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j == 0) {
matrix[i][j] = matrix[i - 1][j + 1];
} else if (j == cols - 1) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = matrix[i - 1][j - 1] + matrix[i - 1][j + 1];
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
System.out.print(" ");
} else {
System.out.print(matrix[i][j] + " ");
}
}
System.out.println();
}
}

public static void main(String[] args) {
PascalTriangle pt = new PascalTriangle();
int numRows = 5;
pt.print(numRows);
}
}
// 1
// 1 0 1
// 1 0 2 0 1
// 1 0 3 0 3 0 1
// 1 0 4 0 6 0 4 0 1