码迷,mamicode.com
首页 > 其他好文 > 详细

Available time

时间:2019-11-17 10:26:40      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:return   blocks   obj   integer   mini   company   potential   nim   VID   

Google Calendar, Outlook, iCal has been banned from your company! So an intrepid engineer has decided to roll their own implementation. Unfortunately one major missing feature is the ability to find out what time slots are free for a particular individual.

Given a list of time blocks where a particular person is already booked/busy, a start and end time to search between, a minimum duration to search for, find all the blocks of time that a person is free for a potential meeting that will last the aforementioned duration.

Given: start_time, end_time, duration, meetings_list -> suggested_meeting_times

Let‘s assume we abstract the representation of times as simple integers, so a valid time is any valid integer supported by your environment. Here is an example input:

meetings_list: [3,20], [-2, 0], [0,2], [16,17], [19,23], [30,40], [27, 33]

start_time: -5

end_time: 27

min_duration: 2

expected answer:

free_time: [-5, -2], [23,27]

Feel free to represent the meetings however you would like, i.e. List of Lists, Lists of Objects etc.

 1 public static List<List<Integer>> scheduler(List<List<Integer>> meetings, int start, int end, int duration) {
 2         List<List<Integer>> ans = new ArrayList<>();
 3         if (duration == 0) return ans;
 4         Collections.sort(meetings, (a, b) -> a.get(0) - b.get(0));
 5         for (List<Integer> meeting : meetings) {
 6             int curEnd = Math.min(meeting.get(0), end);
 7             if (curEnd - start >= duration) {
 8                 ans.add(Arrays.asList(start, curEnd));
 9             }
10             start = Math.max(start, meeting.get(1));
11             if (start >= end)
12                 break;
13         }
14         if (end - start >= duration)
15             ans.add(Arrays.asList(start, end));
16         return ans;
17     }

 

Available time

标签:return   blocks   obj   integer   mini   company   potential   nim   VID   

原文地址:https://www.cnblogs.com/beiyeqingteng/p/11875254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!