You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// There are N processes to be completed. All the processes have a unique number assigned to them from 1 to N.
7
+
8
+
// Now, we are given two things:
9
+
10
+
// 1)The calling order in which all the processes are called.
11
+
// 2)The ideal order in which all the processes should have been executed.
12
+
13
+
// Executing a process takes 1 unit of time. Changing the position takes 1 unit of time.
14
+
15
+
// We have to find out the unit of time required to complete all the process such that a process is executed from the ideal order only when it exists at the same index in the calling order. We can push the first term from the calling order to the last thus rotating the order.
16
+
17
+
// Input Format
18
+
// First line contains a single integer N.
19
+
// Next line contains N space separated integers denoting the calling order.
20
+
// Last line contains N space separated integers denoting the ideal order.
21
+
22
+
// Constraints
23
+
// 1 <= N <= 10^6
24
+
25
+
// Output Format
26
+
// The total time required
27
+
28
+
// Sample Input
29
+
// 5
30
+
// 5 4 2 3 1
31
+
// 5 2 1 4 3
32
+
// Sample Output
33
+
// 7
34
+
// Explanation
35
+
// Iteration #1: Since the ideal order and calling order both has process #5 to be executed first. Process #5 is executed taking 1 unit of time. The new calling order is: 4 - 2 - 3 - 1. Time taken in step #1: 1.
36
+
37
+
// Iteration #2: Since the ideal order has process #2 to be executed firstly, the calling ordered has to be changed again, i.e., the first element has to be pushed to the last place. The new calling order is: 2 - 3 - 1 - 4 and process #2 is executed. Time taken in step #2: 2.
38
+
39
+
// Iteration #3: Since the ideal order has process #1 to be executed firstly, the calling ordered has to be changed again, i.e., the first element has to be pushed to the last place. The new calling order is: 1 - 4 - 3 and process #1 is executed. Time taken in step #2: 2.
40
+
41
+
// Iteration #4: Since the new first element of the calling order is same as the ideal order, that process will be executed. Time taken in step #4: 1.
42
+
43
+
// Iteration #5: Since the last element of the calling order is same as the ideal order, that process will be executed. Time taken in step #5: 1.
0 commit comments