計算與給定會議時間相交的時間間隔
問題陳述
我們給定一個二維陣列,其中包含 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