目录
问题陈述
示例示例
输入
输出
Explanation
解释
方法一
算法
Example
示例
首页 后端开发 C++ 计算与给定会议时间相交的区间数

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

Aug 31, 2023 pm 08:13 PM
会议时间相交区间数

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

问题陈述

我们已经给出了一个包含起始和结束时间对的二维数组,表示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 <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小时制,然后只需进行正常的比较。

以上是计算与给定会议时间相交的区间数的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

C标准模板库(STL)如何工作? C标准模板库(STL)如何工作? Mar 12, 2025 pm 04:50 PM

本文解释了C标准模板库(STL),重点关注其核心组件:容器,迭代器,算法和函子。 它详细介绍了这些如何交互以启用通用编程,提高代码效率和可读性t

如何有效地使用STL(排序,查找,转换等)的算法? 如何有效地使用STL(排序,查找,转换等)的算法? Mar 12, 2025 pm 04:52 PM

本文详细介绍了c中有效的STL算法用法。 它强调了数据结构选择(向量与列表),算法复杂性分析(例如,std :: sort vs. std vs. std :: partial_sort),迭代器用法和并行执行。 常见的陷阱

我如何在C中有效处理异常? 我如何在C中有效处理异常? Mar 12, 2025 pm 04:56 PM

本文详细介绍了C中的有效异常处理,涵盖了尝试,捕捉和投掷机制。 它强调了诸如RAII之类的最佳实践,避免了不必要的捕获块,并为强大的代码登录例外。 该文章还解决了Perf

如何使用C中的移动语义来提高性能? 如何使用C中的移动语义来提高性能? Mar 18, 2025 pm 03:27 PM

本文讨论了使用C中的移动语义来通过避免不必要的复制来提高性能。它涵盖了使用std :: Move的实施移动构造函数和任务运算符,并确定了关键方案和陷阱以有效

在C中如何有效地使用RVALUE参考? 在C中如何有效地使用RVALUE参考? Mar 18, 2025 pm 03:29 PM

文章讨论了在C中有效使用RVALUE参考,以进行移动语义,完美的转发和资源管理,重点介绍最佳实践和性能改进。(159个字符)

如何在C 20中使用范围进行更有表现的数据操纵? 如何在C 20中使用范围进行更有表现的数据操纵? Mar 17, 2025 pm 12:58 PM

C 20范围通过表现力,合成性和效率增强数据操作。它们简化了复杂的转换并集成到现有代码库中,以提高性能和可维护性。

动态调度如何在C中起作用,如何影响性能? 动态调度如何在C中起作用,如何影响性能? Mar 17, 2025 pm 01:08 PM

本文讨论了C中的动态调度,其性能成本和优化策略。它突出了动态调度会影响性能并将其与静态调度进行比较的场景,强调性能和之间的权衡

C的内存管理如何工作,包括新,删除和智能指针? C的内存管理如何工作,包括新,删除和智能指针? Mar 17, 2025 pm 01:04 PM

C内存管理使用新的,删除和智能指针。本文讨论了手册与自动化管理以及智能指针如何防止内存泄漏。

See all articles