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

Method Using Sorting (Sort array of string using comparator)

Arrays.sort(nums,(a,b)->
{
    if(a.length() !=b.length())
    {
        return a.length()-b.length(); // different length
    }
    else
    {
        return a.compareTo(b); // same size scneario// sort lexographically
    }
});
        return nums[n-k];
    }

method using priority Queue

import java.util.PriorityQueue;

class Solution {
    public String kthLargestNumber(String[] nums, int k) {

        // Min Heap with custom comparator for comparing numbers represented as strings
        PriorityQueue<String> pq = new PriorityQueue<>((a, b) -> {
            if (a.length() != b.length())
                return a.length() - b.length();   // shorter = smaller
            return a.compareTo(b);               // if same length, compare lexicographically
        });

        // Add each number to the heap and maintain size = k
        for (String num : nums) {
            pq.add(num);
            if (pq.size() > k) {
                pq.poll(); // remove the smallest
            }
        }

        // The root of the heap is the k-th largest number
        return pq.peek();
    }
}

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence