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

MediumArrayStringDivide and ConquerQuickselectSortingHeap (Priority Queue)
Problem
#1985
Reading time
2 min

The problem

You are given an array of strings nums and an integer k. Each string in nums represents an integer without leading zeros.

Example

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"].

Python solution

Python
class Solution:
    def kthLargestNumber(self, nums: List[str], k: int) -> str:
        return nlargest(k, nums, key=lambda x: int(x))[k - 1]

Complexity

MeasureComplexity
TimeO(n \times \log n) or O(n), where n is the length of the \textit{nums} array
SpaceO(\log n) or O(1) auxiliary

Pattern: Heap / Priority Queue

Keep only the best k elements, or always pull the smallest, in log time. LeetCode 1985. Find the Kth Largest Integer in the Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Heap (Priority Queue).

The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1985. Find the Kth Largest Integer in the Array?
LeetCode 1985. Find the Kth Largest Integer in the Array is rated Medium on LeetCode.
What topics does LeetCode 1985. Find the Kth Largest Integer in the Array cover?
LeetCode 1985. Find the Kth Largest Integer in the Array is tagged Array, String, Divide and Conquer, Quickselect, Sorting and Heap (Priority Queue) on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview