示例示例输入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 <bits/stdc++.h>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 >= 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小时制,然后只需进行正常的比较。
以上就是计算与给定会议时间相交的区间数的详细内容。
