python合并时间

This is a popular algorithm problem that involves a list of integers which indicate meeting time slots in a calendar. The target is to return a merged list by merging the overlapping time slots.

这是一个流行的算法问题,涉及一个整数列表 ,这些整数表示日历中的会议时隙。 目标是通过合并重叠的时隙来返回合并列表。

Found it on InterviewCake. Although their descriptive solution was super helpful, the lack of visuals is what prompted me to write up this post. Since I Love Python, I am sticking to jargon specific to it. All of it should be pretty transferable to any other programming language!

在InterviewCake上找到它。 尽管他们的描述性解决方案非常有帮助,但缺乏视觉效果促使我撰写了这篇文章。 由于我喜欢Python,因此我坚持使用专门针对它的术语。 所有这些都应该可以很好地移植到任何其他编程语言中!

Considering the time slots start from 9:00am, let’s imagine the input contains integers which are 30-minute blocks past 9.

考虑到从上午9:00开始的时间段,让我们想象一下输入中包含的整数是9点后30分钟的整数。

For example:

例如:

(2, 3)  # Meeting from 10:00 – 10:30 am

样本I / O: (Sample I/O:)

输入 : (Input:)

[(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]

(Output)

[(0, 1), (3, 8), (9, 12)]

Let’s analyze the I/O a bit more:

让我们进一步分析I / O:

  • Each tuple in the input list can be considered as specific time slots, for example — (3,5) indicates a meeting taking place from 10:30 to 11:30 (30 min blocks from 9:00) 输入列表中的每个元组都可以视为特定的时隙,例如- (3,5)表示会议从10:30到11:30(从9:00开始30分钟)
  • The input list might not be sorted. Sorting it seems to make life easier to solve this problem! So we already know we doing O(n logn) 输入列表可能未排序。 排序似乎可以使解决该问题的工作变得更轻松! 所以我们已经知道我们在做O(n logn)
  • The output list contains the meeting slots merged and thus the list is more compact

(The Pattern)

The sorting part is so obvious here!

排序部分在这里是如此明显!

  • After sorting we would get the list where all the tuples are sorted by their start time i.e the first index in the tuple. This would give us the leverage of only checking with the end time, since we already know that the start time of the slot at i+1 will always be less than i 排序后,我们将获得一个列表,其中所有元组均按其开始时间进行排序, 即元组中的第一个索引。 这将给我们带来仅检查结束时间的优势,因为我们已经知道,插槽i + 1的开始时间将始终小于i

Next, notice that to merge two consecutive slots, what we can do:

接下来,请注意要合并两个连续的插槽,我们可以执行以下操作:

  • check if the start time of the latter slot is less than the end time of the previous slot. 检查后一个时隙的开始时间是否小于前一个时隙的结束时间 。
  • for example — (0,1), (3,5) cannot be merged since 3 is not less than 1 — meaning that the two slots are not overlapping 例如- (0,1), (3,5)不能合并,因为3不小于1表示两个插槽不重叠
  • conversely — (3,5), (4,8) can be merged since 4 is less than 5 — meaning that the two slots are overlapping 相反- (3,5), (4,8)可以合并,因为4小于5表示两个广告位重叠

Lastly, notice how we pick the end time

最后,请注意我们如何选择结束时间

  • the end time is always the max of the latest merged slot and the current slot’s end time
  • the merged slots can be persisted in a result list that we return

算法 (The Algorithm)

Let’s walk through the algorithm in simple plain English:

让我们用简单的普通英语逐步介绍一下该算法:

  1. Sort by ascending order of start time (first index in each tuple) => O(n logn)
  2. Initialize a merged_list that would serve as the resultant list that we could return in the end 初始化一个merged_list ,它将作为最终可以返回的结果列表
  3. Populate the merged_list with the first meeting slot 在第一个会议槽中填充merged_list
  4. Loop over all the input meeting slots other than the first one (since it’s already inserted)
  5. In each iteration of the loop do the following:
  • get the latest merged start and end time from the merged_list (last index contains the latest, since we are appending)
  • if the current start time is ≤ latest_merged_end, then update the merged_list last index (latest) with (latest_merged_start_time, max(latest_merged_end_time, current_time)) 如果当前开始时间为≤Latest_merged_end,则使用(latest_merged_start_time,max(latest_merged_end_time,current_time))更新merged_list的最后一个索引(最新)。
  • else: append (current_start_time, current_end_time)

(The Code:)

(Visuals:)

Nothing beats visualization!

可视化无与伦比!

python合并多个音频 python怎么合并_python合并多个音频

visualization of the algorithmic step 算法步骤的可视化

翻译自: https://medium.com/@abrarshariargalib/merging-meeting-times-327c3c634fa7

python合并时间