计算与给定会议时间相交的区间数

计算与给定会议时间相交的区间数

问题陈述

我们已经给出了一个包含起始和结束时间对的二维数组,表示12小时制的时间间隔。同时,我们还给出了一个以12小时制表示的字符串 str。我们需要找到包含由 str 表示的时间的总间隔数。

示例示例

输入

arr[][2] = {{“12:02:AM”, “10:55:PM”}, {“12:51:AM”, “11:40:AM”}, {“01:30:AM”, “12:00:PM”}, {“11:57:PM”, “11:59:PM”}}, str = “2:30:AM”

输出

3

Explanation

的中文翻译为:

解释

时间“2:30:AM”与前三个时间间隔相交。

输入

arr[][2] = {{“01:02:PM”, “10:55:PM”}, {“01:30:AM”, “11:00:AM”}}, str = “11:30:PM”

输出

0

Explanation

的中文翻译为:

解释

时间“11:30:PM”与数组中给定的任何时间间隔不相交。

方法一

在这种方法中,我们将把时间转换为24小时制。然后,我们将通过比较来计算与给定时间相交的时间间隔的总数。此外,我们将使用substr()方法来获取子字符串,并使用stoi()方法将字符串转换为整数。

算法

步骤 1 – 定义convertTime()函数,用于将时间转换为24小时制。

步骤 1.1 − 使用replace()方法将第三个位置的冒号替换为空字符串。

步骤 1.2 − 从给定的字符串中获取表示小时的第一个和第二个字符,并通过将第一个数字乘以10再加上第二个数字将其转换为小时。

步骤 1.3 – 将 ‘time_24’ 变量初始化为零。

步骤 1.4 − 我们需要处理两种情况来将时间转换为24小时制。第一种情况是上午,第二种情况是下午。

步骤 1.4.1 – 如果字符串中的第5个字符是’A’,则时间为上午。如果时间为上午,并且小时等于12,则从字符串中仅提取分钟,因为我们将12:00 AM视为00:00小时。否则,将时间字符串转换为整数值。

步骤 1.4.2 – 如果字符串中的第五个字符是 ‘P’,则时间为下午。提取时间字符串,并将其转换为整数。此外,如果小时不等于 12,则将 1200 添加到 ‘time_24’ 变量中。

第二步 – convertTime()函数将以以下格式返回时间。

12:00:AM = 0000

12:58:AM = 0059

11:32:AM = 1132

11:32:PM = 1200 + 1132 = 2332

04:56:PM = 1200 + 456 = 1656

如果字符串中的第5个字符是’A’,则时间为上午。如果时间是上午,并且小时等于12,则从字符串中仅提取分钟,因为我们将12:00 AM视为00:00小时。否则,将时间字符串转换为整数值。

第三步 – 将给定的时间字符串转换为24小时制格式。

第四步 – 使用for循环遍历时间间隔数组,并将每个时间字符串转换为24小时制。

第5步 – 同时,继续检查给定的时间字符串是否在当前间隔之间。如果是,则将’res’的计数增加1。

第六步 – 返回‘res’变量的值。

Example

的翻译为:

示例

#include using namespace std;// Function to convert the given time_24 in 24 hours formatint convertTime(string str){   // Remove the colon from the string   str.replace(2, 1, "");   // Stores the hour   int char_h1 = (int)str[1] - '0';   int char_h2 = (int)str[0] - '0';   int hours = (char_h2 * 10 + char_h1 % 10);   // variable to store the time in 24 hours format   int time_24 = 0;   // If the time is in "AM."   if (str[5] == 'A'){      // If hours are equal to 12, then update it to 0 as 12 AM, and only minutes to time_24      if (hours == 12){         time_24 += stoi(str.substr(2, 2));      } else {         // add hours and minutes to time_24         time_24 += stoi(str.substr(0, 4));      }   }   // If time is in "PM"   else {      // If hours is equal to 12, add time as it is to time_24      if (hours == 12){         time_24 += stoi(str.substr(0, 4));      } else {         // add time to time_24         time_24 += stoi(str.substr(0, 4));         // add 1200 = 12 : 00 PM to time_24 to convert in 24 hours format.         time_24 += 1200;      }   }   return time_24;}// Function to find the total number of intervals that intersects with given meeting time_24int totalIntersects(string arr[][2], int len, string str){   // to store the total number of intervals   int res = 0;   // convert the given time_24 in 24 hours format   int convertedStr = convertTime(str);   // Traverse the array   for (int i = 0; i < len; i++){      // convert the starting time_24 of the current interval in 24-hour format      int initial = convertTime(arr[i][0]);      // convert the ending time_24 of the current interval in 24-hour format      int end = convertTime(arr[i][1]);      // If the given time_24 lies in the interval [initial, end], then increment res by 1      if ((initial <= convertedStr && convertedStr = end && convertedStr <= initial))         res++;   }   // Return res   return res;}int main(){   string arr[][2] = {{"11:00:AM", "11:55:PM"},                       {"12:19:AM", "9:30:AM"},                       {"12:51:AM", "12:59:PM"},                       {"6:57:AM", "7:50:PM"}};   string str = "12:54:AM";   int len = sizeof(arr) / sizeof(arr[0]);   cout << "The total number of the interval that intersects with given meeting time_24 are - " << totalIntersects(arr, len, str) << endl;}

输出

The total number of the interval that intersects with given meeting time_24 are - 2

时间复杂度 – O(N),因为我们遍历时间间隔的数组。

空间复杂度 − O(1),因为我们不使用常量空间。

在解决上述问题时,用户应主要关注将时间转换为24小时制,然后只需进行正常的比较。

以上就是计算与给定会议时间相交的区间数的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:08:01
下一篇 2025年12月15日 07:30:58

发表回复

登录后才能评论
关注微信