From 47af7d3b5d43814e397839434b386f9e221d5659 Mon Sep 17 00:00:00 2001 From: Abinash Karmakar Date: Thu, 1 Aug 2024 20:00:06 +0530 Subject: [PATCH] Add some code --- .../java/build-a-matrix-with-conditions.java | 54 +++++++++++++++++++ .../problems/java/count-number-of-teams.java | 25 +++++++++ .../create-binary-tree-from-descriptions.java | 21 ++++++++ .../java/delete-nodes-and-return-forest.java | 27 ++++++++++ .../java/filling-bookcase-shelves.java | 17 ++++++ ...-of-neighbors-at-a-threshold-distance.java | 39 ++++++++++++++ .../java/lucky-numbers-in-a-matrix.java | 27 ++++++++++ ...mum-deletions-to-make-string-balanced.java | 16 ++++++ .../java/number-of-senior-citizens.java | 11 ++++ ...ond-minimum-time-to-reach-destination.java | 45 ++++++++++++++++ leetcode/problems/java/sort-an-array.java | 6 +++ .../sort-array-by-increasing-frequency.java | 20 +++++++ .../java/sort-the-jumbled-numbers.java | 26 +++++++++ ...ns-from-a-binary-tree-node-to-another.java | 21 ++++++++ 14 files changed, 355 insertions(+) create mode 100644 leetcode/problems/java/build-a-matrix-with-conditions.java create mode 100644 leetcode/problems/java/count-number-of-teams.java create mode 100644 leetcode/problems/java/create-binary-tree-from-descriptions.java create mode 100644 leetcode/problems/java/delete-nodes-and-return-forest.java create mode 100644 leetcode/problems/java/filling-bookcase-shelves.java create mode 100644 leetcode/problems/java/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java create mode 100644 leetcode/problems/java/lucky-numbers-in-a-matrix.java create mode 100644 leetcode/problems/java/minimum-deletions-to-make-string-balanced.java create mode 100644 leetcode/problems/java/number-of-senior-citizens.java create mode 100644 leetcode/problems/java/second-minimum-time-to-reach-destination.java create mode 100644 leetcode/problems/java/sort-an-array.java create mode 100644 leetcode/problems/java/sort-array-by-increasing-frequency.java create mode 100644 leetcode/problems/java/sort-the-jumbled-numbers.java create mode 100644 leetcode/problems/java/step-by-step-directions-from-a-binary-tree-node-to-another.java diff --git a/leetcode/problems/java/build-a-matrix-with-conditions.java b/leetcode/problems/java/build-a-matrix-with-conditions.java new file mode 100644 index 0000000..a6a064e --- /dev/null +++ b/leetcode/problems/java/build-a-matrix-with-conditions.java @@ -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[] adj = new ArrayList[n + 1]; + for (int i = 0; i <= n; i++) { + adj[i] = new ArrayList(); + } + 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 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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/count-number-of-teams.java b/leetcode/problems/java/count-number-of-teams.java new file mode 100644 index 0000000..893a99b --- /dev/null +++ b/leetcode/problems/java/count-number-of-teams.java @@ -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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/create-binary-tree-from-descriptions.java b/leetcode/problems/java/create-binary-tree-from-descriptions.java new file mode 100644 index 0000000..e17a160 --- /dev/null +++ b/leetcode/problems/java/create-binary-tree-from-descriptions.java @@ -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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/delete-nodes-and-return-forest.java b/leetcode/problems/java/delete-nodes-and-return-forest.java new file mode 100644 index 0000000..15c0ee3 --- /dev/null +++ b/leetcode/problems/java/delete-nodes-and-return-forest.java @@ -0,0 +1,27 @@ +import java.util.*; + +class Solution { + public List delNodes(TreeNode root, int[] to_delete) { + List result = new ArrayList<>(); + Set toDelete = new HashSet<>(); + for (int i : to_delete) { + toDelete.add(i); + } + deleteNodes(root, toDelete, result, true); + return result; + } + + private TreeNode deleteNodes(TreeNode node, Set toDelete, List 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; + } + +} \ No newline at end of file diff --git a/leetcode/problems/java/filling-bookcase-shelves.java b/leetcode/problems/java/filling-bookcase-shelves.java new file mode 100644 index 0000000..54de13c --- /dev/null +++ b/leetcode/problems/java/filling-bookcase-shelves.java @@ -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]; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java b/leetcode/problems/java/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java new file mode 100644 index 0000000..b7e11ee --- /dev/null +++ b/leetcode/problems/java/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java @@ -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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/lucky-numbers-in-a-matrix.java b/leetcode/problems/java/lucky-numbers-in-a-matrix.java new file mode 100644 index 0000000..f7f92b9 --- /dev/null +++ b/leetcode/problems/java/lucky-numbers-in-a-matrix.java @@ -0,0 +1,27 @@ +import java.util.*; + +class Solution { + public List luckyNumbers(int[][] matrix) { + List 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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/minimum-deletions-to-make-string-balanced.java b/leetcode/problems/java/minimum-deletions-to-make-string-balanced.java new file mode 100644 index 0000000..43080b6 --- /dev/null +++ b/leetcode/problems/java/minimum-deletions-to-make-string-balanced.java @@ -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]; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/number-of-senior-citizens.java b/leetcode/problems/java/number-of-senior-citizens.java new file mode 100644 index 0000000..0e550c4 --- /dev/null +++ b/leetcode/problems/java/number-of-senior-citizens.java @@ -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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/second-minimum-time-to-reach-destination.java b/leetcode/problems/java/second-minimum-time-to-reach-destination.java new file mode 100644 index 0000000..bc18b70 --- /dev/null +++ b/leetcode/problems/java/second-minimum-time-to-reach-destination.java @@ -0,0 +1,45 @@ +import java.util.*; + +class Solution { + public int secondMinimum(int n, int[][] edges, int time, int change) { + @SuppressWarnings("unchecked") + List[] 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 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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/sort-an-array.java b/leetcode/problems/java/sort-an-array.java new file mode 100644 index 0000000..f951422 --- /dev/null +++ b/leetcode/problems/java/sort-an-array.java @@ -0,0 +1,6 @@ +class Solution { + public int[] sortArray(int[] nums) { + Arrays.sort(nums); + return nums; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/sort-array-by-increasing-frequency.java b/leetcode/problems/java/sort-array-by-increasing-frequency.java new file mode 100644 index 0000000..3971c2e --- /dev/null +++ b/leetcode/problems/java/sort-array-by-increasing-frequency.java @@ -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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/sort-the-jumbled-numbers.java b/leetcode/problems/java/sort-the-jumbled-numbers.java new file mode 100644 index 0000000..9fcbae3 --- /dev/null +++ b/leetcode/problems/java/sort-the-jumbled-numbers.java @@ -0,0 +1,26 @@ +import java.util.*; + +class Solution { + public int[] sortJumbled(int[] mapping, int[] nums) { + List 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; + } +} \ No newline at end of file diff --git a/leetcode/problems/java/step-by-step-directions-from-a-binary-tree-node-to-another.java b/leetcode/problems/java/step-by-step-directions-from-a-binary-tree-node-to-another.java new file mode 100644 index 0000000..32fd02a --- /dev/null +++ b/leetcode/problems/java/step-by-step-directions-from-a-binary-tree-node-to-another.java @@ -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); + } +} \ No newline at end of file