📜  计算与给定会议时间相交的间隔

📅  最后修改于: 2021-09-03 13:43:58             🧑  作者: Mango

给定一个数组arr[][2]N对表示开始和结束时间的字符串(以 12 小时格式)和一个表示会议时间的字符串P组成,任务是找到包含时间P。

例子:

方法:思路是先把所有的时间从12小时制转换成24小时制,然后和时间P比较范围。请按照以下步骤解决问题:

  • 首先将所有12小时格式的时间转换为24小时格式,然后存储24小时格式的整数值。
  • 初始化一个变量,比如ans,来存储P所在区间的计数。
  • 如果(P ≥ L && P ≤ R)(P ≥ R && P ≤ L) ,则遍历数组arr[]并将ans的计数增加1
  • 最后,完成上述步骤后,打印ans

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to convert a time in 24
// hour format to an equivalent integer
int convert(string str)
{
    // Removes ":" at 3rd position
    str.replace(2, 1, "");
   
    // Calculate hours
    int h1 = (int)str[1] - '0';
    int h2 = (int)str[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    // Stores the time in 24 hours format
    int time = 0;
 
    // If time is in "AM"
    if (str[5] == 'A') {
 
        // If hh is equal to 12
        if (hh == 12)
            time += stoi(str.substr(2, 2));
 
        else {
            time += stoi(str.substr(0, 2));
        }
    }
 
    // If time is in "PM"
    else {
 
        // If hh is equal to 12
        if (hh == 12) {
           
            time += stoi(str.substr(0, 4));
        }
        else {
 
            time += stoi(str.substr(0, 4));
            time += 1200;
        }
    }
 
    // Return time
    return time;
}
// Function to count number
// of intervals in which p lies
int countOverlap(string arr[][2],
                 int n, string p)
{
    // Stores the count
    int ans = 0;
 
    // Stores the integer value of
    // 24 hours time format of P
    int M = convert(p);
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Stores the integer value of
        // 24 hours time format of arr[i][0]
        int L = convert(arr[i][0]);
 
        // Stores the integer value of
        // 24 hours time format of arr[i][1]
        int R = convert(arr[i][1]);
 
        // If M lies within the [L, R]
        if ((L <= M && M <= R)
            || (M >= R && M <= L))
            // Increment ans by 1
            ans++;
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    string arr[][2] = { { "12:00:AM", "11:55:PM" },
                        { "12:01:AM", "11:50:AM" },
                        { "12:30:AM", "12:00:PM" },
                        { "11:57:AM", "11:59:PM" } };
    string P = "12:01:PM";
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << countOverlap(arr, N, P) << endl;
}


Java
// Java implementation of the above approach
import java.io.*;
class GFG
{
 
    // Function to convert a time in 24
    // hour format to an equivalent integer
    static int convert(String str)
    {
       
        // Removes ":" at 3rd position
        str = str.substring(0, 2) + str.substring(3);
 
        // Calculate hours
        int h1 = (int)str.charAt(1) - '0';
        int h2 = (int)str.charAt(0) - '0';
        int hh = (h2 * 10 + h1 % 10);
 
        // Stores the time in 24 hours format
        int time = 0;
 
        // If time is in "AM"
        if (str.charAt(5) == 'A') {
 
            // If hh is equal to 12
            if (hh == 12)
                time += Integer.parseInt(
                    str.substring(2, 4));
 
            else {
                time += Integer.parseInt(
                    str.substring(0, 2));
            }
        }
 
        // If time is in "PM"
        else {
 
            // If hh is equal to 12
            if (hh == 12) {
                time += Integer.parseInt(
                    str.substring(0, 4));
            }
            else {
 
                time += Integer.parseInt(
                    str.substring(0, 4));
                time += 1200;
            }
        }
 
        // Return time
        return time;
    }
   
    // Function to count number
    // of intervals in which p lies
    static int countOverlap(String arr[][], int n, String p)
    {
       
        // Stores the count
        int ans = 0;
 
        // Stores the integer value of
        // 24 hours time format of P
        int M = convert(p);
 
        // Traverse the array
        for (int i = 0; i < n; i++)
        {
 
            // Stores the integer value of
            // 24 hours time format of arr[i][0]
            int L = convert(arr[i][0]);
 
            // Stores the integer value of
            // 24 hours time format of arr[i][1]
            int R = convert(arr[i][1]);
 
            // If M lies within the [L, R]
            if ((L <= M && M <= R) || (M >= R && M <= L))
               
                // Increment ans by 1
                ans++;
        }
 
        // Return ans
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String[][] arr
            = new String[][] { { "12:00:AM", "11:55:PM" },
                               { "12:01:AM", "11:50:AM" },
                               { "12:30:AM", "12:00:PM" },
                               { "11:57:AM", "11:59:PM" } };
        String P = "12:01:PM";
        int N = arr.length;
 
        System.out.println(countOverlap(arr, N, P));
    }
}
 
// This code is contributed by Dharanendra L V


C#
// C# implementation of the above approach
using System;
 
class GFG{
 
// Function to convert a time in 24
// hour format to an equivalent integer
static int convert(String str)
{
     
    // Removes ":" at 3rd position
    str = str.Substring(0, 2) + str.Substring(3);
 
    // Calculate hours
    int h1 = (int)str[1] - '0';
    int h2 = (int)str[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    // Stores the time in 24 hours format
    int time = 0;
 
    // If time is in "AM"
    if (str[5] == 'A')
    {
         
        // If hh is equal to 12
        if (hh == 12)
            time += Int32.Parse(
                str.Substring(2, 2));
 
        else
        {
            time += Int32.Parse(
                str.Substring(0, 2));
        }
    }
 
    // If time is in "PM"
    else
    {
         
        // If hh is equal to 12
        if (hh == 12)
        {
            time += Int32.Parse(
                str.Substring(0, 4));
        }
        else
        {
            time += Int32.Parse(
                str.Substring(0, 4));
            time += 1200;
        }
    }
 
    // Return time
    return time;
}
 
// Function to count number
// of intervals in which p lies
static int countOverlap(String [,]arr, int n,
                        String p)
{
   
    // Stores the count
    int ans = 0;
 
    // Stores the integer value of
    // 24 hours time format of P
    int M = convert(p);
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Stores the integer value of
        // 24 hours time format of arr[i,0]
        int L = convert(arr[i,0]);
 
        // Stores the integer value of
        // 24 hours time format of arr[i,1]
        int R = convert(arr[i,1]);
 
        // If M lies within the [L, R]
        if ((L <= M && M <= R) ||
            (M >= R && M <= L))
           
            // Increment ans by 1
            ans++;
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    String[,] arr = new String[,]{
         { "12:00:AM", "11:55:PM" },
         { "12:01:AM", "11:50:AM" },
         { "12:30:AM", "12:00:PM" },
         { "11:57:AM", "11:59:PM" } };
    String P = "12:01:PM";
    int N = arr.GetLength(0);
 
    Console.WriteLine(countOverlap(arr, N, P));
}
}
 
// This code is contributed by 29AjayKumar


输出:
2

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