1985. Find the Kth Largest Integer in the Array LeetCode Solution

In this guide, you will get 1985. Find the Kth Largest Integer in the Array LeetCode Solution with the best time and space complexity. The solution to Find the Kth Largest Integer in the Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Kth Largest Integer in the Array solution in C++
  4. Find the Kth Largest Integer in the Array solution in Java
  5. Find the Kth Largest Integer in the Array solution in Python
  6. Additional Resources
1985. Find the Kth Largest Integer in the Array LeetCode Solution image

Problem Statement of Find the Kth Largest Integer in the Array

You are given an array of strings nums and an integer k. Each string in nums represents an integer without leading zeros.
Return the string that represents the kth largest integer in nums.
Note: Duplicate numbers should be counted distinctly. For example, if nums is [“1″,”2″,”2”], “2” is the first largest integer, “2” is the second-largest integer, and “1” is the third-largest integer.

Example 1:

Input: nums = [“3″,”6″,”7″,”10”], k = 4
Output: “3”
Explanation:
The numbers in nums sorted in non-decreasing order are [“3″,”6″,”7″,”10”].
The 4th largest integer in nums is “3”.

Example 2:

Input: nums = [“2″,”21″,”12″,”1”], k = 3
Output: “2”
Explanation:
The numbers in nums sorted in non-decreasing order are [“1″,”2″,”12″,”21”].
The 3rd largest integer in nums is “2”.

Example 3:

Input: nums = [“0″,”0”], k = 2
Output: “0”
Explanation:
The numbers in nums sorted in non-decreasing order are [“0″,”0”].
The 2nd largest integer in nums is “0”.

See also  1472. Design Browser History LeetCode Solution

Constraints:

1 <= k <= nums.length <= 104
1 <= nums[i].length <= 100
nums[i] consists of only digits.
nums[i] will not have any leading zeros.

Complexity Analysis

  • Time Complexity: O(n\log k)
  • Space Complexity: O(k)

1985. Find the Kth Largest Integer in the Array LeetCode Solution in C++

class Solution {
 public:
  // Similar to 215. Kth Largest Element in an Array
  string kthLargestNumber(vector<string>& nums, int k) {
    auto compare = [](const string& a, const string& b) {
      return a.length() == b.length() ? a > b : a.length() > b.length();
    };
    priority_queue<string, vector<string>, decltype(compare)> minHeap(compare);

    for (const string& num : nums) {
      minHeap.push(num);
      if (minHeap.size() > k)
        minHeap.pop();
    }

    return minHeap.top();
  }
};
/* code provided by PROGIEZ */

1985. Find the Kth Largest Integer in the Array LeetCode Solution in Java

class Solution {
  // Similar to 215. Kth Largest Element in an Array
  public String kthLargestNumber(String[] nums, int k) {
    Queue<String> minHeap = new PriorityQueue<>(
        (a, b) //
        -> a.length() == b.length() ? a.compareTo(b) : Integer.compare(a.length(), b.length()));

    for (final String num : nums) {
      minHeap.offer(num);
      if (minHeap.size() > k)
        minHeap.poll();
    }

    return minHeap.poll();
  }
}
// code provided by PROGIEZ

1985. Find the Kth Largest Integer in the Array LeetCode Solution in Python

class Solution:
  # Similar to 215. Kth Largest Element in an Array
  def kthLargestNumber(self, nums: list[str], k: int) -> str:
    minHeap = []

    for num in nums:
      heapq.heappush(minHeap, int(num))
      if len(minHeap) > k:
        heapq.heappop(minHeap)

    return str(minHeap[0])
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.