计算与给定会议时间相交的时间间隔
问题陈述
我们给定一个二维数组,其中包含 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
解释
时间“2:30:AM”与前三个时间间隔相交。
输入
arr[][2] = {{“01:02:PM”, “10:55:PM”},
{“01:30:AM”, “11:00:AM”}}, str = “11:30:PM”
输出
0
解释
时间“11:30:PM”与数组中给定的任何时间间隔都不相交。
方法 1
在这种方法中,我们将时间转换为 24 小时格式。之后,我们将通过比较计算与给定时间相交的时间间隔总数。此外,我们将使用 substr() 方法获取子字符串,并使用 stoi() 方法将字符串转换为整数。
算法
步骤 1 − 定义 convertTime() 函数以将时间转换为 24 小时格式。
步骤 1.1 − 使用 replace() 方法将第三个位置的冒号替换为空字符串。
步骤 1.2 − 从给定字符串中获取表示小时的第一和第二个字符,并通过将第一个数字乘以 10 并加上第二个数字来将它们转换为小时。
步骤 1.3 − 将“time_24”变量初始化为零。
步骤 1.4 − 我们需要处理两种情况以将时间转换为 24 小时。第一个是 AM,第二个是 PM。
步骤 1.4.1 − 如果字符串中的第 5 个字符是“A”,则时间为 AM。如果时间为 AM,并且小时等于 12,则仅从字符串中提取分钟,因为我们将 12:00 AM 视为 00:00 小时。否则,将时间字符串转换为整数值。
步骤 1.4.2 − 如果字符串中的第 5 个字符是“P”,则时间为 PM。提取时间字符串,并将其转换为整数。此外,如果小时不等于 12,则将 1200 添加到“time_24”变量中。
步骤 2 − 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”,则时间为 AM。如果时间为 AM,并且小时等于 12,则仅从字符串中提取分钟,因为我们将 12:00 AM 视为 00:00 小时。否则,将时间字符串转换为整数值。
步骤 3 − 将给定的时间字符串转换为 24 小时格式。
步骤 4 − 使用 for 循环遍历时间间隔数组并将每个时间字符串转换为 24 小时格式。
步骤 5 − 此外,不断检查给定的时间字符串是否在当前间隔内。如果是,则将“res”的计数增加 1。
步骤 6 − 返回“res”变量的值。
示例
#include <bits/stdc++.h>
using namespace std;
// Function to convert the given time_24 in 24 hours format
int 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_24
int 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 小时格式,之后,他们只需要进行正常的比较。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP