Monk and Power of Time

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 nint calling[], int ideal[])
{
    queue<intq;
    for (int i = 0i < ni++)
    {
        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] = {321};
    int ideal[n] = {132};

    cout << monkPowerTime(ncallingideal<< "\n";

    return 0;
}


No comments:

Post a Comment