520. Find the Kth Largest Integer in the Array

MediumBinary SearchSortingQuickselect

Given an array of strings, each representing a non-negative integer that may be very large (no leading zeros except the value "0"), return the k-th largest integer (1-indexed) as a string. Compare by numeric value: a longer string is larger; for equal lengths compare lexicographically. The input is JSON {nums, k}.

Input: JSON {nums, k}.

Output: Quoted string — the k-th largest integer.

Examples

Example 1
Input: {"nums":["3","6","7","10"],"k":4}
Output: "3"
Explanation: Sorted desc: 10,7,6,3; 4th is 3.
Example 2
Input: {"nums":["2","21","12","1"],"k":3}
Output: "2"
Explanation: Desc: 21,12,2,1; 3rd is 2.
Example 3
Input: {"nums":["0","0"],"k":2}
Output: "0"
Explanation: Both zero.

Constraints

Asked by

GoogleMeta
Solve this problem in the editor →