Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1985.html

1985. Find the Kth Largest Integer in the Array

Level

Medium

Description

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.

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”.

Constraints:

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

Solution

Since all numbers in nums will not have any leading zeros, treat the strings as numbers. To compare two numbers, first compare the lengths and then compare the contents if they have the same length. Sort nums according to the numbers in ascending order, and return the k-th element from the end of nums.

  • class Solution {
        public String kthLargestNumber(String[] nums, int k) {
            Arrays.sort(nums, new Comparator<String>() {
                public int compare(String num1, String num2) {
                    if (num1.length() != num2.length())
                        return num1.length() - num2.length();
                    else
                        return num1.compareTo(num2);
                }
            });
            return nums[nums.length - k];
        }
    }
    
    ############
    
    class Solution {
        public String kthLargestNumber(String[] nums, int k) {
            Arrays.sort(
                nums, (a, b) -> a.length() == b.length() ? b.compareTo(a) : b.length() - a.length());
            return nums[k - 1];
        }
    }
    
  • // OJ: https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/
    // Time: O(NlogN * W)
    // Space: O(1)
    class Solution {
        bool isLessThan(string &a, string &b) {
            int N = a.size();
            for (int i = 0; i < N; ++i) {
                if (a[i] == b[i]) continue;
                return a[i] < b[i];
            }
            return false;
        }
    public:
        string kthLargestNumber(vector<string>& A, int k) {
            sort(begin(A), end(A), [&](auto &a, auto &b) {
                return a.size() != b.size() ? a.size() < b.size() : isLessThan(a, b);
            });
            return A[A.size() - k];
        }
    };
    
  • class Solution:
        def kthLargestNumber(self, nums: List[str], k: int) -> str:
            def cmp(a, b):
                if len(a) != len(b):
                    return len(b) - len(a)
                return 1 if b > a else -1
    
            nums.sort(key=cmp_to_key(cmp))
            return nums[k - 1]
    
    ############
    
    # 1985. Find the Kth Largest Integer in the Array
    # https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/
    
    class Solution:
        def kthLargestNumber(self, nums: List[str], k: int) -> str:
            nums.sort(key = lambda x : int(x))
    
            return nums[len(nums) - k]
    
    
  • func kthLargestNumber(nums []string, k int) string {
    	sort.Slice(nums, func(i, j int) bool {
    		a, b := nums[i], nums[j]
    		if len(a) == len(b) {
    			return a > b
    		}
    		return len(a) > len(b)
    	})
    	return nums[k-1]
    }
    

All Problems

All Solutions