class Solution {
public:
string kthLargestNumber(vector<string>& nums, int k) {
priority_queue<int>q;
for(int i=0;i<nums.size();i++)
{
q.push(stoi(nums[i]));
}
while(!q.empty())
{
k--;
if(k==0)
{
return q.top();
}
q.pop();
}
return 0;
}
};
I was trying to solve Leetcode 1985. Find the Kth Largest Integer in the Array problem. But I am having a Compile Error at line no 14 ( return q.top(); ) I don't know why this happening.
Just looking at the code, the answer would be simple. The function should return an
std::stringand you return andint. You could be tempted now to solve the issue by using theto_stringfunction and convert theintto astd:string.But, this will not work.
The solution should and must work with strings.
It is obvious that you cannot store 100 digits long numbers in an
int. So, you must also use sorting with strings.But this will make life difficult, because you then need to employ so called natural sorting.
Example: Looking a 2,3 and 200, then, if you compare string, 3 will be greater than 200. And that is what the guys want from you. Natural sorting.
There are many solutions possible. Here is one of them: