Skip to content

Commit

Permalink
Add some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Aug 1, 2024
1 parent fd5418f commit 47af7d3
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 0 deletions.
54 changes: 54 additions & 0 deletions leetcode/problems/java/build-a-matrix-with-conditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.*;

class Solution {

public int[][] buildMatrix(
int k,
int[][] rowConditions,
int[][] colConditions) {
int[] orderRows = topoSort(rowConditions, k);
int[] orderColumns = topoSort(colConditions, k);
if (orderRows.length == 0 || orderColumns.length == 0)
return new int[0][0];
int[][] matrix = new int[k][k];
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
if (orderRows[i] == orderColumns[j]) {
matrix[i][j] = orderRows[i];
}
}
}
return matrix;
}

private int[] topoSort(int[][] edges, int n) {
@SuppressWarnings("unchecked")
List<Integer>[] adj = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<Integer>();
}
int[] deg = new int[n + 1], order = new int[n];
int idx = 0;
for (int[] x : edges) {
adj[x[0]].add(x[1]);
deg[x[1]]++;
}
Queue<Integer> q = new LinkedList<>();
for (int i = 1; i <= n; i++) {
if (deg[i] == 0)
q.offer(i);
}
while (!q.isEmpty()) {
int f = q.poll();
order[idx++] = f;
n--;
for (int v : adj[f]) {
if (--deg[v] == 0)
q.offer(v);
}
}
if (n != 0)
return new int[0];
return order;
}
}
25 changes: 25 additions & 0 deletions leetcode/problems/java/count-number-of-teams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int numTeams(int[] rating) {
int n = rating.length;
int count = 0;
for (int i = 0; i < n; i++) {
int leftSmaller = 0, leftLarger = 0, rightSmaller = 0, rightLarger = 0;
for (int j = 0; j < i; j++) {
if (rating[j] < rating[i]) {
leftSmaller++;
} else if (rating[j] > rating[i]) {
leftLarger++;
}
}
for (int j = i + 1; j < n; j++) {
if (rating[j] < rating[i]) {
rightSmaller++;
} else if (rating[j] > rating[i]) {
rightLarger++;
}
}
count += leftSmaller * rightLarger + leftLarger * rightSmaller;
}
return count;
}
}
21 changes: 21 additions & 0 deletions leetcode/problems/java/create-binary-tree-from-descriptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public TreeNode createBinaryTree(int[][] descriptions) {
TreeNode[] map = new TreeNode[100001];
boolean[] child = new boolean[100001];
for (int[] d : descriptions) {
if (map[d[0]] == null)
map[d[0]] = new TreeNode(d[0]);
TreeNode node = (map[d[1]] == null ? new TreeNode(d[1]) : map[d[1]]);
if (d[2] == 1)
map[d[0]].left = node;
else
map[d[0]].right = node;
map[node.val] = node;
child[d[1]] = true;
}
for (int[] d : descriptions)
if (!child[d[0]])
return map[d[0]];
return null;
}
}
27 changes: 27 additions & 0 deletions leetcode/problems/java/delete-nodes-and-return-forest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.*;

class Solution {
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
List<TreeNode> result = new ArrayList<>();
Set<Integer> toDelete = new HashSet<>();
for (int i : to_delete) {
toDelete.add(i);
}
deleteNodes(root, toDelete, result, true);
return result;
}

private TreeNode deleteNodes(TreeNode node, Set<Integer> toDelete, List<TreeNode> result, boolean isRoot) {
if (node == null) {
return null;
}
boolean deleted = toDelete.contains(node.val);
if (isRoot && !deleted) {
result.add(node);
}
node.left = deleteNodes(node.left, toDelete, result, deleted);
node.right = deleteNodes(node.right, toDelete, result, deleted);
return deleted ? null : node;
}

}
17 changes: 17 additions & 0 deletions leetcode/problems/java/filling-bookcase-shelves.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int minHeightShelves(int[][] books, int shelfWidth) {
int n = books.length;
int[] dp = new int[n + 1];
for (int i = 1; i <= n; ++i) {
int w = books[i - 1][0];
int h = books[i - 1][1];
dp[i] = dp[i - 1] + h;
for (int j = i - 1; j > 0 && w + books[j - 1][0] <= shelfWidth; --j) {
w += books[j - 1][0];
h = Math.max(h, books[j - 1][1]);
dp[i] = Math.min(dp[i], dp[j - 1] + h);
}
}
return dp[n];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*;

class Solution {
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
int[][] dist = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(dist[i], Integer.MAX_VALUE);
dist[i][i] = 0;
}
for (int[] edge : edges) {
dist[edge[0]][edge[1]] = edge[2];
dist[edge[1]][edge[0]] = edge[2];
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] != Integer.MAX_VALUE && dist[k][j] != Integer.MAX_VALUE) {
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
int min = Integer.MAX_VALUE;
int res = -1;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (dist[i][j] <= distanceThreshold) {
count++;
}
}
if (count <= min) {
min = count;
res = i;
}
}
return res;
}
}
27 changes: 27 additions & 0 deletions leetcode/problems/java/lucky-numbers-in-a-matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.*;

class Solution {
public List<Integer> luckyNumbers(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;
int[] rowMin = new int[m];
int[] colMax = new int[n];
Arrays.fill(rowMin, Integer.MAX_VALUE);
Arrays.fill(colMax, Integer.MIN_VALUE);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
rowMin[i] = Math.min(rowMin[i], matrix[i][j]);
colMax[j] = Math.max(colMax[j], matrix[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == rowMin[i] && matrix[i][j] == colMax[j]) {
result.add(matrix[i][j]);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int minimumDeletions(String s) {
int n = s.length();
int[] f = new int[n + 1];
int b = 0;
for (int i = 1; i <= n; ++i) {
if (s.charAt(i - 1) == 'b') {
f[i] = f[i - 1];
++b;
} else {
f[i] = Math.min(f[i - 1] + 1, b);
}
}
return f[n];
}
}
11 changes: 11 additions & 0 deletions leetcode/problems/java/number-of-senior-citizens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int countSeniors(String[] details) {
int ans = 0;
for (var x : details) {
int age = Integer.parseInt(x.substring(11, 13));
if (age > 60)
++ans;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.*;

class Solution {
public int secondMinimum(int n, int[][] edges, int time, int change) {
@SuppressWarnings("unchecked")
List<Integer>[] graph = new ArrayList[n + 1];
for (int i = 0; i < n + 1; i++)
graph[i] = new ArrayList<>();
for (int[] edge : edges) {
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
}

Deque<int[]> deque = new LinkedList<>();
int[] visitedNum = new int[n + 1];
int[] timeArr = new int[n + 1];
Arrays.fill(timeArr, -1);
deque.offerLast(new int[] { 1, 0 });
timeArr[0] = 0;

while (deque.size() > 0) {
int curSize = deque.size();
for (int i = 0; i < curSize; i++) {
int[] cur = deque.pollFirst();
int nextTime = 0;
int curLight = cur[1] / change;
if (curLight % 2 == 0)
nextTime = cur[1] + time;
else
nextTime = (curLight + 1) * change + time;

for (int nextNode : graph[cur[0]]) {
if (visitedNum[nextNode] < 2 && timeArr[nextNode] < nextTime) {
deque.offerLast(new int[] { nextNode, nextTime });
visitedNum[nextNode]++;
timeArr[nextNode] = nextTime;
if (nextNode == n && visitedNum[nextNode] == 2)
return nextTime;
}
}
}
}
return -1;
}
}
6 changes: 6 additions & 0 deletions leetcode/problems/java/sort-an-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public int[] sortArray(int[] nums) {
Arrays.sort(nums);
return nums;
}
}
20 changes: 20 additions & 0 deletions leetcode/problems/java/sort-array-by-increasing-frequency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int[] frequencySort(int[] nums) {
int count[] = new int[201];
for (int n : nums)
count[n + 100]++;
for (int i = nums.length - 1; i >= 0;) {
int m = 0, ind = -1;
for (int j = 0; j < 201; j++) {
if (count[j] > m) {
m = count[j];
ind = j;
}
}
for (int j = 0; j < m; j++)
nums[i--] = ind - 100;
count[ind] = 0;
}
return nums;
}
}
26 changes: 26 additions & 0 deletions leetcode/problems/java/sort-the-jumbled-numbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.*;

class Solution {
public int[] sortJumbled(int[] mapping, int[] nums) {
List<int[]> mappedList = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
String s = Integer.toString(nums[i]);
StringBuilder n = new StringBuilder();
for (char ch : s.toCharArray())
n.append(mapping[ch - '0']);
mappedList.add(new int[] { nums[i], Integer.parseInt(n.toString()), i });
}

mappedList.sort((a, b) -> {
if (a[1] != b[1])
return Integer.compare(a[1], b[1]);
else
return Integer.compare(a[2], b[2]);
});

int[] sortedNums = new int[nums.length];
for (int i = 0; i < mappedList.size(); i++)
sortedNums[i] = mappedList.get(i)[0];
return sortedNums;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
private boolean find(TreeNode n, int val, StringBuilder sb) {
if (n.val == val)
return true;
if (n.left != null && find(n.left, val, sb))
sb.append("L");
else if (n.right != null && find(n.right, val, sb))
sb.append("R");
return sb.length() > 0;
}

public String getDirections(TreeNode root, int startValue, int destValue) {
StringBuilder s = new StringBuilder(), d = new StringBuilder();
find(root, startValue, s);
find(root, destValue, d);
int i = 0, max_i = Math.min(d.length(), s.length());
while (i < max_i && s.charAt(s.length() - i - 1) == d.charAt(d.length() - i - 1))
++i;
return "U".repeat(s.length() - i) + d.reverse().toString().substring(i);
}
}

0 comments on commit 47af7d3

Please sign in to comment.