Skip to content

Commit

Permalink
Add some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed May 17, 2024
1 parent dcabb80 commit 3e5430c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
11 changes: 11 additions & 0 deletions leetcode/problems/java/delete-leaves-with-a-given-value.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public TreeNode removeLeafNodes(TreeNode root, int target) {
if (root == null)
return null;
root.left = removeLeafNodes(root.left, target);
root.right = removeLeafNodes(root.right, target);
if (root.val == target && root.left == null && root.right == null)
return null;
return root;
}
}
18 changes: 18 additions & 0 deletions leetcode/problems/java/k-closest-points-to-origin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.PriorityQueue;

class Solution {
public int[][] kClosest(int[][] points, int k) {
PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> b[0] * b[0] + b[1] * b[1] - a[0] * a[0] - a[1] * a[1]);
for (int[] point : points) {
maxHeap.add(point);
if (maxHeap.size() > k) {
maxHeap.poll();
}
}
int[][] result = new int[k][2];
while (k-- > 0) {
result[k] = maxHeap.poll();
}
return result;
}
}
28 changes: 28 additions & 0 deletions leetcode/problems/java/kth-largest-element-in-a-stream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.PriorityQueue;

class KthLargest {
PriorityQueue<Integer> pq;
int k;

public KthLargest(int k, int[] nums) {
pq = new PriorityQueue<>(k);
this.k = k;
for (int num : nums) {
add(num);
}
}

public int add(int val) {
pq.add(val);
if (pq.size() > k) {
pq.poll();
}
return pq.peek();
}
}

/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/
14 changes: 14 additions & 0 deletions leetcode/problems/java/kth-largest-element-in-an-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.PriorityQueue;

class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int num : nums) {
minHeap.add(num);
if (minHeap.size() > k) {
minHeap.remove();
}
}
return minHeap.remove();
}
}
19 changes: 19 additions & 0 deletions leetcode/problems/java/last-stone-weight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.PriorityQueue;
import java.util.Collections;

class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for (int stone : stones) {
maxHeap.add(stone);
}
while (maxHeap.size() > 1) {
int stone1 = maxHeap.remove();
int stone2 = maxHeap.remove();
if (stone1 != stone2) {
maxHeap.add(stone1 - stone2);
}
}
return maxHeap.isEmpty() ? 0 : maxHeap.remove();
}
}

0 comments on commit 3e5430c

Please sign in to comment.