-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
dcabb80
commit 3e5430c
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
leetcode/problems/java/delete-leaves-with-a-given-value.java
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,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; | ||
} | ||
} |
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,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
28
leetcode/problems/java/kth-largest-element-in-a-stream.java
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,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
14
leetcode/problems/java/kth-largest-element-in-an-array.java
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,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(); | ||
} | ||
} |
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,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(); | ||
} | ||
} |