[No Audio]
Consider an office environment where there is
a boss and he has two secretaries. He needs
these services of the secretaries from time to time.
The secretaries are also involved in
other activities, and therefore has their own
schedule of meetings. Some of the meetings of
the secretaries may be overlapping. The boss
wants to know the time slots in which both
the secretaries may be busy, that is both
have meetings. This will help him in finding
out a time slot in which at least one of the
secretary will be free, so that some tasks may
be assigned to him. To accomplish this, we
need to find the overlapping times of the
secretaries. Let us start implementing this problem.
We will assume that the meeting
schedule for the two secretaries are given to
us in the form of factors. Each meeting is
associated with the start and end times, which
is also given in the form of factors.
Let us declare a couple of variables which will
contain the meeting timings for the two employees.
[No Audio]
Each employee meeting schedules are given by
vectors, and the entries in the vectors are
showing the time slots in which the meeting
will be held. The first value represents the
starting time and the second value represents
the ending time of the meeting. The times in
this case are in 24 hours system. For
example, the value 13 means 1pm in the
afternoon, and the value 15 will mean 3pm in
the afternoon. Please note that, to access the
starting or ending time information for a
specific meeting, we will use the syntax of
couple of square brackets.
The index value in
the first square bracket will corresponds to
the meeting number, and the index in the
second square bracket will correspond to
either the starting or ending times.
Next, we will implement a function for determining the
overlapping or intersecting meeting timings.
[No Audio]
The input to this function will be the
vectors containing the meeting information of
the two secretaries, which are of type vectors of vectors.
[No Audio]
The output will be the vector
containing the overlapping timings, and will
be of type vector of vectors.
[No Audio]
First, I will declare an empty variable which
will contain the overlapping timings.
I will return this variable, so therefore
its type will also be that of vectors of vectors.
I will also mention its name without
a semicolon to tell Rust compiler,
that it is a returning value.
[No Audio]
Next, we will iterate through all the
meetings of a secretary a, and we'll check it
with all the meetings of secretary b, one by
one to see if they have an overlap. We will
use a couple of loops for this purpose. The
outer loop will be used to iterate through
all the meetings of secretary a,
and the inner loop will iterate through all
the meetings of secretary b.
[No Audio]
The logic in the loop will work by
considering the first meeting of secretary a,
which will be checked with all the meetings
of secretary b. Then the second meeting of
secretary a will be checked with all the
meetings of secretary b, and so on.
The variable i will correspond to meetings of a,
and the variable j will correspond to meeting of b.
To check the overlap of meetings, we
will first compute the starting and ending
times of the meetings.
[No Audio]
The first level of square brackets
corresponds to the meeting number, and the
second level of index is used to reflect the
starting or ending time. For the secretary a
we use, the index of outer, the index of outer for loop
which is i, and then the index 0
which corresponds to the starting time, and
for the secretary b we use the index of j,
which corresponds to the inner for loop.
In the same way, we will also capture the ending
times for the meetings of the two secretaries.
[No Audio]
When this code executes, the first meeting of
secretary a will be checked with all the
meetings of the secretary b, and then the
second meeting of secretary a will be
checked against all the meetings of secretary
b, and so on. Next we will determine if the
two meetings has an overlap or not. I will
declare another function for this purpose called overlap.
[No Audio]
This function will have four input
values of type i32, which represents the
starting and ending times of the meetings of
the two secretaries. The output will be an
optionVector containing the overlapping
time information. First, I will declare a
variable intersection_time, which
will contain the intersection times if any,
and I will initialize it as an empty vector.
[No Audio]
Next, we need to define the conditions for
the overlap. In this case, we have two
intervals of values, and we need to determine
if the interval has an overlap. Let us now
learn how we can determine the overlap
between intervals using some visuals.
[No Audio]
The rectangles represents the meetings and the
values inside it are the starting and ending
times of the meeting. We have at least three
possible cases. In the first case, we do not
have an overlap. In the second case, we have
an overlap of one hour. In the third case,
again, we have an overlap of one hour, but
with a difference that the meeting of b
starts first followed by the meeting of a.
The essential condition for the two meetings
to have an overlap is that, one of the meeting
should not end before the start of the other.
This condition may be checked by checking
that the maximum of the starting times of the
two meetings is less than the minimum of the
ending times of the two meetings,
let us look into this condition.
[No Audio]
In the first case, the
maximum of the two starting times that is
7 and 10 is the value 10. However, the
minimum of the ending times, that is 9 and 12 is 9.
Since 10 is not less than 9,
therefore, the two meetings does not have an
overlap. However, in case of 2 and 3,
there is an overlap, because this condition is
being true for them. For instance, in case 2,
the maximum of the starting times is 9, and
the minimum of the ending times is 10. Since
9 is less than 10, therefore, they will
have an overlap. The lower end of the overlap
interval will be given by the maximum of the
starting times, and the lower end of the
interval will be the minimum of the ending times.
Let us code this now. I will first
write a condition for comparing the maximum
of the starting values with that of the
minimum of the ending values. I will use the
max and min functions from the standard
library, so I will first include that and
then I will write the conditions.
[No Audio]
If the condition is true, then I will store
the lower and ending and of the interval in
the intersection_time. First I will push the lower end,
[No Audio]
and then the upper end of the interval.
[No Audio]
In case there is no overlap, so I will return the None.
[No Audio]
Let us now use this function in the
overlapping_meeting function.
I will declare a variable overlap_status,
and we'll assign to it the returning
value from the function of overlap.
[No Audio]
I will next check if we have some overlap or not.
[No Audio]
If we have an overlap, then I
will add the overlapping interval to the
intersection vector.
[No Audio]
I need to wrap the value,
because it is wrapped inside the Some variant
of the option enum.
Finally, we will call the
overlapping_meetings function in
the main with the vectors containing the
meetings information for the two secretaries,
and will assign its result to a variable.
[No Audio]
I will add a print statement to display
the slots which has an overlap.
[No Audio]
Let us cargo run.
[No Audio]
We have two slots of overlapping timings.
If we look at the first meeting of
secretary a, then indeed it has an overlap
with the first meeting of secretary b from 14 till 15.
In the same way, the last meeting of
secretary a, and the second meeting of
secretary b, also has an overlap from 7 to 9.
This overlapping information can now be
used by the boss to determine the time slots
in which at least one of his secretary may be free.
That is it for this particular segment,
we have looked at an example, and while
solving and implementing the example we were
able to practice multi-dimensional vectors
and nested loops. In the next example, we
will be learning the use of HashSet by
implementing another real life problem. Do
come back for that and until next tutorial,
happy Rust programming.
[No Audio]