Skip to content

Latest commit

 

History

History
43 lines (35 loc) · 1.28 KB

find-the-kth-largest-integer-in-the-array.md

File metadata and controls

43 lines (35 loc) · 1.28 KB

Find the Kth Largest Integer in the Array

Problem Link

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"

Example 2

Input: nums = ["2","21","12","1"], k = 3
Output: "2"

Solution

class Solution {
public:
    string kthLargestNumber(vector<string>& nums, int k) {
        sort(nums.begin(), nums.end(), [&](string a, string b) {
            if(a.size() != b.size()) {
                return a.size() < b.size();
            }
            else {
                return a < b;
            }
        });
        for(auto n: nums) cout << n << " ";
        return nums.end()[-k];
    }
};

Accepted

image