1 00:00:00,000 --> 00:00:07,900 [No Audio] 2 00:00:07,901 --> 00:00:10,200 Consider an office environment where there is 3 00:00:10,201 --> 00:00:13,333 a boss and he has two secretaries. He needs 4 00:00:13,334 --> 00:00:16,033 these services of the secretaries from time to time. 5 00:00:16,633 --> 00:00:18,700 The secretaries are also involved in 6 00:00:18,701 --> 00:00:21,300 other activities, and therefore has their own 7 00:00:21,301 --> 00:00:24,466 schedule of meetings. Some of the meetings of 8 00:00:24,467 --> 00:00:27,566 the secretaries may be overlapping. The boss 9 00:00:27,567 --> 00:00:30,433 wants to know the time slots in which both 10 00:00:30,434 --> 00:00:33,000 the secretaries may be busy, that is both 11 00:00:33,001 --> 00:00:36,166 have meetings. This will help him in finding 12 00:00:36,200 --> 00:00:38,700 out a time slot in which at least one of the 13 00:00:38,701 --> 00:00:41,633 secretary will be free, so that some tasks may 14 00:00:41,634 --> 00:00:45,100 be assigned to him. To accomplish this, we 15 00:00:45,101 --> 00:00:47,133 need to find the overlapping times of the 16 00:00:47,134 --> 00:00:50,700 secretaries. Let us start implementing this problem. 17 00:00:51,200 --> 00:00:52,566 We will assume that the meeting 18 00:00:52,567 --> 00:00:54,900 schedule for the two secretaries are given to 19 00:00:54,901 --> 00:00:57,966 us in the form of factors. Each meeting is 20 00:00:58,000 --> 00:01:00,533 associated with the start and end times, which 21 00:01:00,534 --> 00:01:02,800 is also given in the form of factors. 22 00:01:03,600 --> 00:01:06,300 Let us declare a couple of variables which will 23 00:01:06,301 --> 00:01:08,966 contain the meeting timings for the two employees. 24 00:01:08,967 --> 00:01:17,933 [No Audio] 25 00:01:17,966 --> 00:01:20,466 Each employee meeting schedules are given by 26 00:01:20,467 --> 00:01:22,733 vectors, and the entries in the vectors are 27 00:01:22,734 --> 00:01:24,666 showing the time slots in which the meeting 28 00:01:24,667 --> 00:01:27,433 will be held. The first value represents the 29 00:01:27,434 --> 00:01:29,633 starting time and the second value represents 30 00:01:29,634 --> 00:01:32,533 the ending time of the meeting. The times in 31 00:01:32,534 --> 00:01:35,933 this case are in 24 hours system. For 32 00:01:35,934 --> 00:01:38,433 example, the value 13 means 1pm in the 33 00:01:38,434 --> 00:01:41,100 afternoon, and the value 15 will mean 3pm in 34 00:01:41,101 --> 00:01:44,700 the afternoon. Please note that, to access the 35 00:01:44,701 --> 00:01:46,766 starting or ending time information for a 36 00:01:46,767 --> 00:01:49,500 specific meeting, we will use the syntax of 37 00:01:49,501 --> 00:01:51,100 couple of square brackets. 38 00:01:52,300 --> 00:01:53,500 The index value in 39 00:01:53,501 --> 00:01:55,366 the first square bracket will corresponds to 40 00:01:55,367 --> 00:01:57,700 the meeting number, and the index in the 41 00:01:57,701 --> 00:01:59,833 second square bracket will correspond to 42 00:02:00,200 --> 00:02:02,100 either the starting or ending times. 43 00:02:02,900 --> 00:02:05,266 Next, we will implement a function for determining the 44 00:02:05,267 --> 00:02:08,500 overlapping or intersecting meeting timings. 45 00:02:08,501 --> 00:02:14,766 [No Audio] 46 00:02:14,767 --> 00:02:16,700 The input to this function will be the 47 00:02:16,733 --> 00:02:18,933 vectors containing the meeting information of 48 00:02:18,934 --> 00:02:22,700 the two secretaries, which are of type vectors of vectors. 49 00:02:22,701 --> 00:02:24,800 [No Audio] 50 00:02:24,801 --> 00:02:26,100 The output will be the vector 51 00:02:26,101 --> 00:02:28,733 containing the overlapping timings, and will 52 00:02:28,734 --> 00:02:31,500 be of type vector of vectors. 53 00:02:31,501 --> 00:02:38,566 [No Audio] 54 00:02:38,567 --> 00:02:40,966 First, I will declare an empty variable which 55 00:02:40,967 --> 00:02:42,700 will contain the overlapping timings. 56 00:02:43,633 --> 00:02:45,666 I will return this variable, so therefore 57 00:02:45,667 --> 00:02:48,333 its type will also be that of vectors of vectors. 58 00:02:49,000 --> 00:02:50,900 I will also mention its name without 59 00:02:50,901 --> 00:02:52,866 a semicolon to tell Rust compiler, 60 00:02:52,867 --> 00:02:54,500 that it is a returning value. 61 00:02:54,501 --> 00:03:00,266 [No Audio] 62 00:03:00,267 --> 00:03:02,000 Next, we will iterate through all the 63 00:03:02,001 --> 00:03:05,566 meetings of a secretary a, and we'll check it 64 00:03:05,567 --> 00:03:08,400 with all the meetings of secretary b, one by 65 00:03:08,401 --> 00:03:12,033 one to see if they have an overlap. We will 66 00:03:12,034 --> 00:03:14,366 use a couple of loops for this purpose. The 67 00:03:14,367 --> 00:03:16,233 outer loop will be used to iterate through 68 00:03:16,234 --> 00:03:18,866 all the meetings of secretary a, 69 00:03:18,867 --> 00:03:20,700 and the inner loop will iterate through all 70 00:03:20,701 --> 00:03:22,500 the meetings of secretary b. 71 00:03:22,501 --> 00:03:28,500 [No Audio] 72 00:03:28,501 --> 00:03:30,300 The logic in the loop will work by 73 00:03:30,301 --> 00:03:32,700 considering the first meeting of secretary a, 74 00:03:32,701 --> 00:03:34,466 which will be checked with all the meetings 75 00:03:34,467 --> 00:03:37,433 of secretary b. Then the second meeting of 76 00:03:37,434 --> 00:03:39,533 secretary a will be checked with all the 77 00:03:39,534 --> 00:03:42,100 meetings of secretary b, and so on. 78 00:03:42,633 --> 00:03:45,466 The variable i will correspond to meetings of a, 79 00:03:45,533 --> 00:03:48,600 and the variable j will correspond to meeting of b. 80 00:03:49,133 --> 00:03:52,300 To check the overlap of meetings, we 81 00:03:52,301 --> 00:03:54,066 will first compute the starting and ending 82 00:03:54,067 --> 00:03:55,466 times of the meetings. 83 00:03:55,467 --> 00:04:02,833 [No Audio] 84 00:04:02,834 --> 00:04:04,533 The first level of square brackets 85 00:04:04,534 --> 00:04:06,266 corresponds to the meeting number, and the 86 00:04:06,267 --> 00:04:08,433 second level of index is used to reflect the 87 00:04:08,434 --> 00:04:12,166 starting or ending time. For the secretary a 88 00:04:12,167 --> 00:04:16,300 we use, the index of outer, the index of outer for loop 89 00:04:16,301 --> 00:04:19,033 which is i, and then the index 0 90 00:04:19,034 --> 00:04:21,166 which corresponds to the starting time, and 91 00:04:21,167 --> 00:04:23,547 for the secretary b we use the index of j, 92 00:04:23,548 --> 00:04:25,500 which corresponds to the inner for loop. 93 00:04:26,200 --> 00:04:28,300 In the same way, we will also capture the ending 94 00:04:28,301 --> 00:04:30,533 times for the meetings of the two secretaries. 95 00:04:30,534 --> 00:04:36,000 [No Audio] 96 00:04:36,001 --> 00:04:38,100 When this code executes, the first meeting of 97 00:04:38,101 --> 00:04:40,133 secretary a will be checked with all the 98 00:04:40,134 --> 00:04:42,266 meetings of the secretary b, and then the 99 00:04:42,267 --> 00:04:44,666 second meeting of secretary a will be 100 00:04:44,700 --> 00:04:46,733 checked against all the meetings of secretary 101 00:04:46,734 --> 00:04:50,133 b, and so on. Next we will determine if the 102 00:04:50,134 --> 00:04:52,500 two meetings has an overlap or not. I will 103 00:04:52,501 --> 00:04:55,533 declare another function for this purpose called overlap. 104 00:04:55,534 --> 00:05:00,766 [No Audio] 105 00:05:00,767 --> 00:05:03,300 This function will have four input 106 00:05:03,301 --> 00:05:05,666 values of type i32, which represents the 107 00:05:05,667 --> 00:05:07,833 starting and ending times of the meetings of 108 00:05:07,834 --> 00:05:11,100 the two secretaries. The output will be an 109 00:05:11,101 --> 00:05:13,233 optionVector containing the overlapping 110 00:05:13,234 --> 00:05:17,000 time information. First, I will declare a 111 00:05:17,001 --> 00:05:19,400 variable intersection_time, which 112 00:05:19,401 --> 00:05:21,766 will contain the intersection times if any, 113 00:05:21,866 --> 00:05:24,500 and I will initialize it as an empty vector. 114 00:05:24,501 --> 00:05:29,100 [No Audio] 115 00:05:29,101 --> 00:05:31,100 Next, we need to define the conditions for 116 00:05:31,101 --> 00:05:33,366 the overlap. In this case, we have two 117 00:05:33,367 --> 00:05:35,966 intervals of values, and we need to determine 118 00:05:35,967 --> 00:05:39,133 if the interval has an overlap. Let us now 119 00:05:39,134 --> 00:05:41,533 learn how we can determine the overlap 120 00:05:41,534 --> 00:05:43,500 between intervals using some visuals. 121 00:05:43,501 --> 00:05:46,500 [No Audio] 122 00:05:46,501 --> 00:05:48,466 The rectangles represents the meetings and the 123 00:05:48,467 --> 00:05:50,600 values inside it are the starting and ending 124 00:05:50,601 --> 00:05:54,600 times of the meeting. We have at least three 125 00:05:54,601 --> 00:05:56,966 possible cases. In the first case, we do not 126 00:05:56,967 --> 00:05:59,166 have an overlap. In the second case, we have 127 00:05:59,167 --> 00:06:02,000 an overlap of one hour. In the third case, 128 00:06:02,033 --> 00:06:04,300 again, we have an overlap of one hour, but 129 00:06:04,301 --> 00:06:06,000 with a difference that the meeting of b 130 00:06:06,001 --> 00:06:08,300 starts first followed by the meeting of a. 131 00:06:10,100 --> 00:06:12,800 The essential condition for the two meetings 132 00:06:12,801 --> 00:06:14,933 to have an overlap is that, one of the meeting 133 00:06:14,934 --> 00:06:17,433 should not end before the start of the other. 134 00:06:17,500 --> 00:06:19,600 This condition may be checked by checking 135 00:06:19,601 --> 00:06:21,666 that the maximum of the starting times of the 136 00:06:21,667 --> 00:06:23,833 two meetings is less than the minimum of the 137 00:06:23,834 --> 00:06:25,766 ending times of the two meetings, 138 00:06:25,966 --> 00:06:28,300 let us look into this condition. 139 00:06:28,301 --> 00:06:31,466 [No Audio] 140 00:06:31,467 --> 00:06:32,466 In the first case, the 141 00:06:32,467 --> 00:06:34,700 maximum of the two starting times that is 142 00:06:34,701 --> 00:06:37,300 7 and 10 is the value 10. However, the 143 00:06:37,301 --> 00:06:40,200 minimum of the ending times, that is 9 and 12 is 9. 144 00:06:40,900 --> 00:06:42,933 Since 10 is not less than 9, 145 00:06:42,934 --> 00:06:45,633 therefore, the two meetings does not have an 146 00:06:45,634 --> 00:06:48,700 overlap. However, in case of 2 and 3, 147 00:06:48,733 --> 00:06:50,833 there is an overlap, because this condition is 148 00:06:50,834 --> 00:06:53,333 being true for them. For instance, in case 2, 149 00:06:53,600 --> 00:06:56,400 the maximum of the starting times is 9, and 150 00:06:56,401 --> 00:06:58,766 the minimum of the ending times is 10. Since 151 00:06:58,767 --> 00:07:00,900 9 is less than 10, therefore, they will 152 00:07:00,901 --> 00:07:04,233 have an overlap. The lower end of the overlap 153 00:07:04,234 --> 00:07:06,633 interval will be given by the maximum of the 154 00:07:06,634 --> 00:07:09,533 starting times, and the lower end of the 155 00:07:09,534 --> 00:07:12,033 interval will be the minimum of the ending times. 156 00:07:12,200 --> 00:07:15,000 Let us code this now. I will first 157 00:07:15,001 --> 00:07:17,366 write a condition for comparing the maximum 158 00:07:17,367 --> 00:07:19,100 of the starting values with that of the 159 00:07:19,101 --> 00:07:21,833 minimum of the ending values. I will use the 160 00:07:21,834 --> 00:07:23,600 max and min functions from the standard 161 00:07:23,601 --> 00:07:26,333 library, so I will first include that and 162 00:07:26,334 --> 00:07:28,433 then I will write the conditions. 163 00:07:28,434 --> 00:07:34,333 [No Audio] 164 00:07:34,334 --> 00:07:36,300 If the condition is true, then I will store 165 00:07:36,301 --> 00:07:39,433 the lower and ending and of the interval in 166 00:07:39,434 --> 00:07:43,000 the intersection_time. First I will push the lower end, 167 00:07:43,001 --> 00:07:46,333 [No Audio] 168 00:07:46,334 --> 00:07:48,566 and then the upper end of the interval. 169 00:07:48,567 --> 00:07:54,233 [No Audio] 170 00:07:54,234 --> 00:07:57,000 In case there is no overlap, so I will return the None. 171 00:07:57,001 --> 00:08:00,100 [No Audio] 172 00:08:00,101 --> 00:08:01,733 Let us now use this function in the 173 00:08:01,734 --> 00:08:03,900 overlapping_meeting function. 174 00:08:05,100 --> 00:08:08,133 I will declare a variable overlap_status, 175 00:08:08,233 --> 00:08:10,700 and we'll assign to it the returning 176 00:08:10,701 --> 00:08:12,700 value from the function of overlap. 177 00:08:12,701 --> 00:08:19,100 [No Audio] 178 00:08:19,101 --> 00:08:22,933 I will next check if we have some overlap or not. 179 00:08:22,934 --> 00:08:27,000 [No Audio] 180 00:08:27,001 --> 00:08:28,500 If we have an overlap, then I 181 00:08:28,501 --> 00:08:30,333 will add the overlapping interval to the 182 00:08:30,334 --> 00:08:31,900 intersection vector. 183 00:08:31,901 --> 00:08:35,832 [No Audio] 184 00:08:35,833 --> 00:08:37,265 I need to wrap the value, 185 00:08:37,266 --> 00:08:39,799 because it is wrapped inside the Some variant 186 00:08:39,800 --> 00:08:41,100 of the option enum. 187 00:08:42,633 --> 00:08:43,732 Finally, we will call the 188 00:08:43,733 --> 00:08:45,700 overlapping_meetings function in 189 00:08:45,701 --> 00:08:47,500 the main with the vectors containing the 190 00:08:47,501 --> 00:08:49,833 meetings information for the two secretaries, 191 00:08:50,033 --> 00:08:52,333 and will assign its result to a variable. 192 00:08:52,334 --> 00:08:58,700 [No Audio] 193 00:08:58,701 --> 00:09:00,600 I will add a print statement to display 194 00:09:00,601 --> 00:09:02,400 the slots which has an overlap. 195 00:09:02,401 --> 00:09:06,500 [No Audio] 196 00:09:06,501 --> 00:09:07,900 Let us cargo run. 197 00:09:07,901 --> 00:09:11,000 [No Audio] 198 00:09:11,001 --> 00:09:13,333 We have two slots of overlapping timings. 199 00:09:13,633 --> 00:09:15,666 If we look at the first meeting of 200 00:09:15,667 --> 00:09:18,533 secretary a, then indeed it has an overlap 201 00:09:18,534 --> 00:09:22,600 with the first meeting of secretary b from 14 till 15. 202 00:09:23,000 --> 00:09:24,800 In the same way, the last meeting of 203 00:09:24,801 --> 00:09:27,300 secretary a, and the second meeting of 204 00:09:27,301 --> 00:09:30,933 secretary b, also has an overlap from 7 to 9. 205 00:09:32,300 --> 00:09:34,233 This overlapping information can now be 206 00:09:34,234 --> 00:09:36,433 used by the boss to determine the time slots 207 00:09:36,434 --> 00:09:39,366 in which at least one of his secretary may be free. 208 00:09:39,866 --> 00:09:41,866 That is it for this particular segment, 209 00:09:41,900 --> 00:09:44,133 we have looked at an example, and while 210 00:09:44,134 --> 00:09:46,433 solving and implementing the example we were 211 00:09:46,434 --> 00:09:48,633 able to practice multi-dimensional vectors 212 00:09:48,634 --> 00:09:51,600 and nested loops. In the next example, we 213 00:09:51,601 --> 00:09:53,633 will be learning the use of HashSet by 214 00:09:53,634 --> 00:09:56,266 implementing another real life problem. Do 215 00:09:56,267 --> 00:09:58,300 come back for that and until next tutorial, 216 00:09:58,333 --> 00:09:59,933 happy Rust programming. 217 00:09:59,934 --> 00:10:04,700 [No Audio]