给定一个由N 个字符串组成的数组arr[]和一个由Q 个查询组成的数组Query[] 。数组arr[]和Query[]中的每个字符串都采用D/M/Y形式,其中D 、 M和Y表示日期、月份和年份。对于每个查询,任务是从给定的数组arr[]打印下一个最近的日期。如果不存在这样的日期,则打印“-1” 。
例子:
Input: arr[]={“22/4/1233”, “1/3/633”, “23/5/56645”, “4/12/233”}, Q = 2,
Query[] = {“23/3/4345”, “12/3/2”}
Output:
23/5/56645
4/12/233
Explanation:
Query 1: The closest date after “23/3/4345” is “23/5/56645”.
Query 2: TThe closest date after “12/3/2” is “4/12/233”.
Input: arr[]={“22/4/1233”, “4/12/233”, “1/3/633”, “23/5/56645”}, Q = 1,
Query[] = {“4/4/34234234”}
Output: -1
朴素方法:对于数组Query[]中的每个查询,最简单的方法是遍历数组arr[]并针对每个日期检查它是否大于当前日期以及是否最接近它。完成数组遍历后,打印获得的最接近的日期。如果未找到日期,则打印-1 。
时间复杂度: O(N*Q)
辅助空间: O(1)
有效的方法:想法是使用比较器函数对给定的数组arr[]进行排序。然后使用二分搜索在Query[] 中找到最接近每个日期的未来日期。请按照以下步骤解决问题:
- 排序日期的阵列由第一比较今年的常用3 [],然后在一个月之后的一天。
- 在上述步骤中对数组进行排序后,对于每个查询,使用二分搜索找到最接近的日期,使用比较器函数比较两个日期。
- 如果找不到有效日期,则打印“-1” 。
- 否则,打印找到的最近日期。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.awt.*;
import java.io.*;
import java.util.*;
class GFG {
// Comparator function to compare
// the two dates
public static int comp(String s,
String t)
{
// Split the dates strings
// when a "/" found
String[] ss = s.split("/");
String[] tt = t.split("/");
int date1[] = new int[3];
int date2[] = new int[3];
// Store the dates in form
// of arrays
for (int i = 0; i < 3; i++) {
date1[i]
= Integer.parseInt(ss[i]);
date2[i]
= Integer.parseInt(tt[i]);
}
// If years are not same
if (date1[2] != date2[2]) {
return date1[2] - date2[2];
}
// If months are not same
else if (date1[1] != date2[1]) {
return date1[1] - date2[1];
}
// If days are not same
else if (date1[0] != date2[0]) {
return date1[0] - date2[0];
}
// If two date is same
return 0;
}
// Function to print the next
// closest date
public static String
nextClosestDate(String arr[],
String q)
{
// Sort date array
Arrays.sort(arr,
new Comparator() {
@Override
public int compare(String o1,
String o2)
{
return comp(o1, o2);
}
});
// Perform the Binary search
// to answer the queries
int l = 0, r = arr.length - 1;
int ind = -1;
// Iterate until l <= r
while (l <= r) {
// Find mid m
int m = (l + r) / 2;
// Comparator function call
int c = comp(q, arr[m]);
// If comp function return 0
// next closest date is found
if (c == 0) {
ind = m;
break;
}
// If comp function return
// less than 0, search in
// the left half
else if (c < 0) {
r = m - 1;
ind = m;
}
// If comp function return
// greater than 0, search
// in the right half
else {
l = m + 1;
}
}
// Return the result
if (ind == -1) {
return "-1";
}
else {
return arr[ind];
}
}
public static void
performQueries(String[] arr,
String[] Q)
{
// Traverse the queries of date
for (int i = 0; i < Q.length; i++) {
// Function Call
System.out.println(
nextClosestDate(arr, Q[i]));
}
}
// Driver Code
public static void main(String[] args)
{
// Given array of dates
String arr[] = { "22/4/1233",
"1/3/633",
"23/5/56645",
"4/12/233" };
// Given Queries
String Q[]
= { "23/3/4345",
"4/4/34234234",
"12/3/2" };
// Function Call
performQueries(arr, Q);
}
}
23/5/56645
-1
4/12/233
时间复杂度: O((N*log N) + (Q*log N))
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live