Find Kth Largest Integer in the Array
You are given an array of strings nums (representing non-negative integers) and integer k. Return the kth largest integer in nums (as a string). Compare by numeric value.
Example 1
Input: nums=["3","6","7","10"], k=4 → Output: "3"
Example 2
Input: nums=["2","21","12","1"], k=3 → Output: "2"
Solution Approach
Use a min-heap of size k (comparing by numeric string length then lexicographically). The top element is the kth largest.
- Use a min-heap with comparator: compare by string length first, then lexicographically.
- Add each string to heap; if size > k, poll minimum.
- Return heap.peek().
Java
import java.util.*;
class Solution {
public String kthLargestNumber(String[] nums, int k) {
PriorityQueue<String> minHeap = new PriorityQueue<>((a, b) ->
a.length() != b.length() ? a.length() - b.length() : a.compareTo(b));
for (String n : nums) {
minHeap.offer(n);
if (minHeap.size() > k) minHeap.poll();
}
return minHeap.peek();
}
}
- Time Complexity: O(N log k)
- Space Complexity: O(k)