1 00:00:00,000 --> 00:00:08,700 [No Audio] 2 00:00:08,701 --> 00:00:11,066 We are working on a project with very tight 3 00:00:11,067 --> 00:00:13,500 time schedule, we want our employees to put 4 00:00:13,501 --> 00:00:15,600 some extra effort. To make sure that the 5 00:00:15,601 --> 00:00:18,633 employee spent their time productively, we 6 00:00:18,634 --> 00:00:21,300 have divided their time schedule in chunks 7 00:00:21,301 --> 00:00:24,900 of two hour slots. The first slot is from 8 00:00:24,901 --> 00:00:27,300 8 to 10 on Monday, the next slot is from 9 00:00:27,301 --> 00:00:30,266 10 to 12 on the same day, and this way, we 10 00:00:30,267 --> 00:00:32,766 have numbered all the slots during the week. 11 00:00:33,233 --> 00:00:35,066 After each time slot, we would like the 12 00:00:35,067 --> 00:00:38,566 employee to tell us what he has produced. The 13 00:00:38,567 --> 00:00:41,300 employees may decide to take some rest 14 00:00:41,301 --> 00:00:43,833 for some slots during the week. However, some 15 00:00:43,834 --> 00:00:45,733 of the employees may work too much in 16 00:00:45,734 --> 00:00:48,700 consecutive time slots, which may negatively 17 00:00:48,733 --> 00:00:51,200 affect their overall productivity and well 18 00:00:51,201 --> 00:00:53,233 being. We would like to determine the 19 00:00:53,234 --> 00:00:56,066 employees who has worked non stop for the 20 00:00:56,067 --> 00:00:59,166 longest time after some interval of time, so 21 00:00:59,167 --> 00:01:02,166 that we may ask to him to have some break, 22 00:01:02,433 --> 00:01:04,300 this is necessary for their overall 23 00:01:04,301 --> 00:01:06,000 productivity and well being. 24 00:01:06,900 --> 00:01:07,866 The input will be 25 00:01:07,867 --> 00:01:09,833 in the form of vector containing numbers 26 00:01:09,834 --> 00:01:12,700 indicating the time slots. The numbers are 27 00:01:12,701 --> 00:01:16,933 not necessarily in order. In this case now, 28 00:01:16,966 --> 00:01:19,866 there are four busy periods for the employees 29 00:01:19,867 --> 00:01:23,066 indicated by different colors. The first busy 30 00:01:23,067 --> 00:01:25,766 period is indicated by the black color 31 00:01:25,767 --> 00:01:29,166 containing the slots of 1 and 2. The next 32 00:01:29,167 --> 00:01:31,533 one is indicated by the green color, which has 33 00:01:31,566 --> 00:01:35,100 a length of three slots, and so on. The highest 34 00:01:35,101 --> 00:01:38,100 length of any busy period in this case, is 3. 35 00:01:38,101 --> 00:01:40,166 Let us code this problem now. 36 00:01:41,733 --> 00:01:44,433 The solution will be based on the use of HashSets, 37 00:01:44,434 --> 00:01:47,500 so we will add the HashSet from the standard library. 38 00:01:47,501 --> 00:01:52,533 [No Audio] 39 00:01:52,534 --> 00:01:54,800 We will start by defining the schedule of all 40 00:01:54,801 --> 00:01:57,466 the employees in the form of vector, I will 41 00:01:57,467 --> 00:01:59,900 use a multi dimensional vectors for this 42 00:01:59,901 --> 00:02:02,500 purpose. Let us have some values representing 43 00:02:02,501 --> 00:02:04,633 the slots in which an employee has worked. 44 00:02:04,634 --> 00:02:15,066 [No Audio] 45 00:02:15,067 --> 00:02:17,533 Each inner vector in this case, represents the 46 00:02:17,534 --> 00:02:19,800 time slot in which an employee has worked. 47 00:02:20,400 --> 00:02:22,500 Next we will define a function to implement 48 00:02:22,501 --> 00:02:23,900 the desired functionality. 49 00:02:23,901 --> 00:02:31,933 [No Audio] 50 00:02:31,934 --> 00:02:33,533 The input to the function will be the 51 00:02:33,534 --> 00:02:36,633 schedule vector that we defined in the main, 52 00:02:36,666 --> 00:02:38,700 which contains the working slots, and the 53 00:02:38,701 --> 00:02:41,400 output will be the employee number, which has 54 00:02:41,401 --> 00:02:43,900 worked nonstop for the longest period, which 55 00:02:43,901 --> 00:02:45,533 will be of type u8. 56 00:02:45,534 --> 00:02:51,366 [No Audio] 57 00:02:51,367 --> 00:02:53,800 First I will define an empty vector, which 58 00:02:53,801 --> 00:02:57,533 will store the longest nonstop work of all the employees. 59 00:02:57,534 --> 00:03:03,000 [No Audio] 60 00:03:03,001 --> 00:03:06,133 Next, we will iterate through each employee's 61 00:03:06,200 --> 00:03:08,200 time slot vector one by one. 62 00:03:08,201 --> 00:03:13,300 [No Audio] 63 00:03:13,301 --> 00:03:17,833 Next for each employee, I will compute his respective 64 00:03:17,834 --> 00:03:21,000 longest term period of work. I will compute 65 00:03:21,001 --> 00:03:24,033 this using another function called longest_period, 66 00:03:24,034 --> 00:03:25,500 so let me define it. 67 00:03:25,501 --> 00:03:30,933 [No Audio] 68 00:03:30,934 --> 00:03:33,200 The input to the function will be 69 00:03:33,201 --> 00:03:35,700 vector of the employee, which I will call 70 00:03:35,701 --> 00:03:38,733 working_slots, and the output will 71 00:03:38,734 --> 00:03:40,833 be the longest_period of slots for which the 72 00:03:40,834 --> 00:03:44,300 employee has worked nonstop, which will be of type u8. 73 00:03:44,301 --> 00:03:47,533 [No Audio] 74 00:03:47,534 --> 00:03:48,933 Inside the function, I will 75 00:03:48,934 --> 00:03:51,033 first declare a variable which will be used 76 00:03:51,034 --> 00:03:53,800 to keep track of the longest busy period, and 77 00:03:53,801 --> 00:03:55,500 I will initialize it from 0. 78 00:03:55,501 --> 00:04:01,200 [No Audio] 79 00:04:01,201 --> 00:04:06,000 Next, we will use a special data type called HashSet. 80 00:04:06,001 --> 00:04:07,400 A HashSet is a 81 00:04:07,401 --> 00:04:10,700 type of HashMap, which will only have keys 82 00:04:10,701 --> 00:04:13,500 and not values. This means, we only need to 83 00:04:13,501 --> 00:04:16,132 define the keys and not the values. It is 84 00:04:16,133 --> 00:04:18,800 useful in situations where we just want to 85 00:04:18,801 --> 00:04:20,700 quickly know if something happens to be 86 00:04:20,701 --> 00:04:23,266 inside a collection or not. This is because 87 00:04:23,267 --> 00:04:26,000 looking for a key in a HashMap is very fast. 88 00:04:26,633 --> 00:04:28,700 The specific reason as to why we choose it 89 00:04:28,701 --> 00:04:31,500 here will become evident to us at the 90 00:04:31,501 --> 00:04:34,466 end of the function definition. I will define 91 00:04:34,467 --> 00:04:37,028 a HashSet, and we'll collect all the time slot 92 00:04:37,029 --> 00:04:38,832 numbers of the employees in it. 93 00:04:39,300 --> 00:04:41,733 This will essentially convert all the values of 94 00:04:41,734 --> 00:04:44,800 the vectors as keys of the HashSet. Now we 95 00:04:44,801 --> 00:04:47,133 will iterate through all the keys in the HashSet. 96 00:04:47,134 --> 00:04:51,633 [No Audio] 97 00:04:51,634 --> 00:04:53,533 First I will iterate through all the 98 00:04:53,534 --> 00:04:54,900 slots one by one. 99 00:04:56,666 --> 00:04:58,500 Okay, let us understand now 100 00:04:58,501 --> 00:05:00,600 the logic that we will use for determining 101 00:05:00,601 --> 00:05:03,266 the consecutive time slots. First we will 102 00:05:03,267 --> 00:05:05,233 identify the beginning of the consecutive 103 00:05:05,234 --> 00:05:07,933 time slots. The essential requirement for a 104 00:05:07,934 --> 00:05:10,133 time slot to be the first time slot in a 105 00:05:10,134 --> 00:05:12,600 sequence is that, there should be no value 106 00:05:12,601 --> 00:05:15,900 lesser by 1 than the value of that time slot. 107 00:05:16,300 --> 00:05:17,966 For instance, if we have the sequence 108 00:05:17,967 --> 00:05:20,966 of let's say 2,3,4, so this means that the 109 00:05:20,967 --> 00:05:24,400 time slot of 1, should not be present in the 110 00:05:24,401 --> 00:05:26,833 HashSet, we will check this condition using 111 00:05:26,834 --> 00:05:28,100 an if statement. 112 00:05:28,101 --> 00:05:32,966 [No Audio] 113 00:05:32,967 --> 00:05:35,166 Please note that, we used a reference since 114 00:05:35,167 --> 00:05:37,733 the contains function accept reference to 115 00:05:37,734 --> 00:05:40,400 the keys. Inside the if, we will keep on 116 00:05:40,401 --> 00:05:42,733 iterating as long as there are next time 117 00:05:42,734 --> 00:05:45,933 slots in the HashSet. I will make a couple of variables. 118 00:05:45,934 --> 00:05:51,400 [No Audio] 119 00:05:51,401 --> 00:05:53,633 The variable current_slot will be 120 00:05:53,634 --> 00:05:56,000 incremented by one inside the loop, and will 121 00:05:56,001 --> 00:05:58,733 be initialized from the first slot value in 122 00:05:58,734 --> 00:06:01,200 the sequence. Since the slot variable is a 123 00:06:01,201 --> 00:06:03,300 reference, so therefore, we need to make an 124 00:06:03,301 --> 00:06:06,800 owned copy of that using the to_owned function. 125 00:06:07,000 --> 00:06:09,600 This function creates owned data from 126 00:06:09,601 --> 00:06:11,800 borrowed data usually by cloning. 127 00:06:12,800 --> 00:06:15,800 The variable current_consecutive_slot 128 00:06:15,801 --> 00:06:16,933 will keep track of the number 129 00:06:16,934 --> 00:06:19,033 of consecutive slots that the employee has 130 00:06:19,034 --> 00:06:21,166 worked. Next I will iterate through the 131 00:06:21,167 --> 00:06:22,600 HashSet as long as there are 132 00:06:22,601 --> 00:06:24,300 next slots in the HashSet. 133 00:06:24,301 --> 00:06:30,700 [No Audio] 134 00:06:30,701 --> 00:06:32,966 Inside the loop, we will increment the 135 00:06:32,967 --> 00:06:35,233 current_slot, so that in the next 136 00:06:35,234 --> 00:06:38,000 iteration it can be checked for presence in the HashSet. 137 00:06:38,001 --> 00:06:43,866 [No Audio] 138 00:06:43,867 --> 00:06:47,900 We will also increment current_consecutive_slot by one. 139 00:06:47,901 --> 00:06:51,800 [No Audio] 140 00:06:51,801 --> 00:06:53,333 Once the loop end, we 141 00:06:53,334 --> 00:06:56,100 will see if the current_consecutive_slot 142 00:06:56,101 --> 00:06:58,666 that we have just found, is 143 00:06:58,667 --> 00:07:01,466 greater than the previous longest_buzy_period. 144 00:07:01,467 --> 00:07:09,500 [No Audio] 145 00:07:09,501 --> 00:07:12,566 If it is, then we will update the longest_buzy_period 146 00:07:12,567 --> 00:07:14,800 from the value of current_consecutive_slot. 147 00:07:14,801 --> 00:07:21,333 [No Audio] 148 00:07:21,334 --> 00:07:23,433 When the loop ends, we will return the 149 00:07:23,434 --> 00:07:26,033 longest_buzy_period, which basically stores the 150 00:07:26,034 --> 00:07:28,200 longest busy period for the employee. 151 00:07:28,201 --> 00:07:34,466 [No Audio] 152 00:07:34,467 --> 00:07:37,166 Okay now let us use this function in the 153 00:07:37,167 --> 00:07:39,633 longest_buzy_time function. 154 00:07:39,933 --> 00:07:41,700 I will call this function for each 155 00:07:41,701 --> 00:07:44,866 employee one by one inside the for loop, and 156 00:07:44,867 --> 00:07:47,433 will store the result in the 157 00:07:47,434 --> 00:07:50,233 employee_longest_nonstop_work vector. 158 00:07:50,234 --> 00:07:59,100 [No Audio] 159 00:07:59,101 --> 00:08:01,500 Next, we will display the longest_nonstop_work 160 00:08:01,501 --> 00:08:03,233 of each employee. 161 00:08:03,234 --> 00:08:11,166 [No Audio] 162 00:08:11,167 --> 00:08:14,100 Finally, I will find out the maximum 163 00:08:14,101 --> 00:08:17,733 longest_nonstop_work of any employee using the max function. 164 00:08:17,734 --> 00:08:24,566 [No Audio] 165 00:08:24,567 --> 00:08:26,866 Finally, we will return the employee number 166 00:08:26,867 --> 00:08:29,300 corresponding to the maximum nonstop work, 167 00:08:29,533 --> 00:08:31,600 this means we need to return the index 168 00:08:31,601 --> 00:08:33,433 corresponding to the maximum value in the 169 00:08:33,434 --> 00:08:36,500 vector, this can be done using the position function. 170 00:08:36,501 --> 00:08:44,200 [No Audio] 171 00:08:44,201 --> 00:08:46,266 Please note that the position function needs 172 00:08:46,267 --> 00:08:48,566 a closure, where we will mention some 173 00:08:48,567 --> 00:08:50,966 condition. The condition in this case is to 174 00:08:50,967 --> 00:08:53,733 check and retrieve the index for the value 175 00:08:54,000 --> 00:08:55,800 which is equal to the maximum value. 176 00:08:56,300 --> 00:08:59,300 The maximum value returned from the above line is, 177 00:08:59,301 --> 00:09:03,000 is wrapped inside the option, so therefore, 178 00:09:03,233 --> 00:09:05,966 we first unwrap the max_val, and 179 00:09:05,967 --> 00:09:08,400 then the position is also wrapped by the 180 00:09:08,401 --> 00:09:10,766 option, so another unwrap will return the 181 00:09:10,767 --> 00:09:13,800 final position. Since the return type is u8, 182 00:09:13,801 --> 00:09:16,266 so we will convert, and this is also the 183 00:09:16,267 --> 00:09:19,433 return statement. Finally, we will add a 184 00:09:19,434 --> 00:09:21,266 suitable print statement inside the main 185 00:09:21,267 --> 00:09:23,800 function, where we will display the employee 186 00:09:23,801 --> 00:09:26,400 with the highest nonstop work by calling the 187 00:09:26,401 --> 00:09:28,600 longest_buzy_time. 188 00:09:28,601 --> 00:09:34,466 [No Audio] 189 00:09:34,467 --> 00:09:35,900 Let us cargo run this. 190 00:09:35,901 --> 00:09:41,000 [No Audio] 191 00:09:41,001 --> 00:09:42,500 That is great. It has 192 00:09:42,501 --> 00:09:45,266 displayed to us the nonstop working hours of 193 00:09:45,267 --> 00:09:48,900 each employee, let us confirm. The employee 1 194 00:09:48,901 --> 00:09:51,666 has worked for three consecutive slots, so his 195 00:09:51,667 --> 00:09:53,866 highest nonstop work is 3 slots. 196 00:09:55,133 --> 00:09:57,800 If we look at the first vector, we can confirm that, 197 00:09:57,801 --> 00:10:01,666 yes, it is the slots of 4,5, and 6. The 198 00:10:01,667 --> 00:10:04,666 final result is that of employee 2, because 199 00:10:04,667 --> 00:10:07,966 he has the highest nonstop slots. That is it 200 00:10:07,967 --> 00:10:10,433 for this particular tutorial. In the upcoming 201 00:10:10,434 --> 00:10:12,700 tutorial, we will be looking at an example 202 00:10:12,701 --> 00:10:14,866 where we will be using the box smart 203 00:10:14,867 --> 00:10:17,566 pointer and binary search trees, that will be 204 00:10:17,567 --> 00:10:19,433 fun and interesting. Do come back for learning 205 00:10:19,434 --> 00:10:22,933 that, and until next tutorial, enjoy Rust programming. 206 00:10:22,934 --> 00:10:27,300 [No Audio]