📜  查询以查找未来的最近日期

📅  最后修改于: 2021-05-14 08:22:10             🧑  作者: Mango

给定一个由N个字符串组成的数组arr []和一个由Q个查询组成的数组Query [] 。数组arr []Query []中的每个字符串的格式为D / M / Y ,其中DMY表示日期,月份和年份。对于每个查询,任务是从给定数组arr []打印下一个最接近的日期。如果没有这样的日期,则打印“ -1”

例子:

天真的方法:对于数组Query []中的每个查询,最简单的方法是遍历数组arr []并针对每个日期,检查它是否大于当前日期,以及是否大于当前日期。遍历数组后,打印获得的最接近日期。如果找不到日期,请打印-1

时间复杂度: O(N * Q)
辅助空间: O(1)

高效的方法:这个想法是使用比较器函数对给定的数组arr []进行排序。然后使用二进制搜索在Query []中找到最接近每个日期的将来日期。请按照以下步骤解决问题:

  1. 排序日期的阵列由第一比较今年的常用3 [],然后在一个月之后的一天
  2. 在上述步骤中对数组进行排序后,对于每个查询,请使用二进制搜索找到最接近的日期,然后使用比较器函数比较两个日期。
  3. 如果找不到有效日期,则打印“ -1”
  4. 否则,打印找到的最接近日期。

下面是上述方法的实现:

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)