在给定的大数数组中找到第 K 个最大的数
给定一个表示大数的字符串数组arr[]和一个整数K,任务是在给定数组中找到第K个最大的整数。
例子:
Input: arr[] = { “10”, “7”, “3”, “6” }, K = 3
Output: “6”
Explanation : Arranging the array in non-decreasing manner will give { “3”, “6”, “7”, “10” }.
Clearly 3rd largest integer is 6.
Input: arr[] = { “10”, “7”, “6”, “7”, “11” }, K = 2
Output: “10”
Explanation: Arranging the array in non-decreasing manner will give { “6”, “7”, “7”, “10”, “11” }
Clearly 2nd largest integer is 10.
朴素方法(不正确):通常此类问题可以通过将字符串转换为整数然后找到第 K 个最大数来解决。但是由于我们谈论的是大数,即表示长度不超过 100的整数的字符串,所以不可能将它们转换为整数。
正确的方法:这个问题可以通过实现自定义比较器使用贪心方法来解决。
请按照以下步骤解决给定的问题。
- 使用自定义排序以非降序对数组进行排序。
- 在自定义排序函数中会有两种情况:
- 两个传递的字符串的大小相等,然后仅当字符串第一个在字典上小于另一个时才交换。
- 传递的两个字符串的大小不相等,只有当一个字符串的大小小于另一个时才交换。
- 排序后打印第 K 个最大的整数。
下面是上述方法的实现:
C++
// C++ implementation for above approach
#include
using namespace std;
// Custom comparator to sort
// the array of strings
bool comp(string& a, string& b)
{
// If sizes of string a and b are equal
// then return true if a is
// lexicographically smaller than b
if (a.size() == b.size())
return a < b;
// Return true if size of a
// is smaller than b
return a.size() < b.size();
}
// Function that returns
// kth largest integer
string kthLargestInteger(
string arr[], int n, int k)
{
// Sort string arr in non-decreasing order
// using custom comparator function
sort(arr, arr + n, comp);
return arr[n - k];
}
// Driver Code
int main()
{
int N = 4;
string arr[N] = { "10", "7", "3", "6" };
int K = 4;
cout << kthLargestInteger(arr, N, K)
<< endl;
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG {
// Custom comparator to sort
// the array of strings
// Function that returns
// kth largest integer
static String kthLargestInteger(String arr[], int n,
int k)
{
// Sort string arr in non-decreasing order
// using custom comparator function
Arrays.sort(arr, new Comparator() {
@Override public int compare(String a, String b)
{
return Integer.valueOf(a).compareTo(
Integer.valueOf(b));
}
});
return arr[n - k];
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
String arr[] = { "10", "7", "3", "6" };
int K = 4;
System.out.println(kthLargestInteger(arr, N, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 implementation for above approach
# Function that returns
# kth largest integer
def kthLargestInteger(
arr, n, k):
# Sort string arr in non-decreasing order
# using custom comparator function
arr = [int(i) for i in arr]
arr.sort()
return arr[n - k]
# Driver Code
if __name__ == "__main__":
N = 4
arr = ["10", "7", "3", "6"]
K = 4
print(kthLargestInteger(arr, N, K))
# This code is contributed by ukasp.
C#
// C# code for the above approach
using System;
public class GFG
{
// Custom comparator to sort
// the array of strings
// Function that returns
// kth largest integer
static String kthLargestint(String []arr, int n,
int k)
{
// Sort string arr in non-decreasing order
// using custom comparator function
Array.Sort(arr,CompareStrings);
return arr[n - k];
}
static int CompareStrings(string a, string b)
{
return Int32.Parse(a).CompareTo(
Int32.Parse(b));
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
String []arr = { "10", "7", "3", "6" };
int K = 4;
Console.WriteLine(kthLargestint(arr, N, K));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
3
时间复杂度: O(N * log N),其中 N 是数组的大小。
辅助空间: O(1)