The Monk is trying to explain to its users that even a single unit of time can be extremely important and to demonstrate this particular fact he gives them a challenging task.
There are N processes to be completed by you, the chosen one since you're Monk's favorite student. All the processes have a unique number assigned to them from 1 to N.
Now, you are given two things:
- The calling order in which all the processes are called.
- The ideal order in which all the processes should have been executed.
Example:
Input: n = 3, calling = [3 ,2, 1], ideal = [1,3,2]
Output: 5
Approach
C++
#include <bits/stdc++.h>using namespace std;int monkPowerTime(int n, int calling[], int ideal[]){queue<int> q;for (int i = 0; i < n; i++){q.push(calling[i]);}int cnt = 0;int i = 0;while (!q.empty()){int x = q.front();q.pop();if (x == ideal[i]){i++;cnt++;}else{q.push(x);cnt++;}}return cnt;}int main(){int n = 3;int calling[n] = {3, 2, 1};int ideal[n] = {1, 3, 2};cout << monkPowerTime(n, calling, ideal) << "\n";return 0;}
No comments:
Post a Comment