blob: 488bdd19df60b22a6d3a2c00072904e340729368 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# 1985. Find the Kth Largest Integer in the Array
import heapq
"""
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 k'th largest integer in 'nums'. duplicate numbers should be
counted distinctly.
"""
class Solution(object):
def kthLargestNumber(self, nums, k):
"""
:type nums: List[str]
:type k: int
:rtype: str
"""
max_heap = [-int(x) for x in nums]
heapq.heapify(max_heap)
while k > 1:
heapq.heappop(max_heap)
k -= 1
return str(-max_heap[0])
if __name__ == "__main__":
obj = Solution()
print(obj.kthLargestNumber(nums=["3", "6", "7", "10"], k=4))
print(obj.kthLargestNumber(nums=["2", "21", "12", "1"], k=3))
print(obj.kthLargestNumber(nums=["0", "0"], k=2))
|