Revolving Door in C++

C++Server Side ProgrammingProgramming

Suppose we have a list of requests, where requests[i] contains [t, d] indicating at time t, a person arrived at the door and either wanted to go inside (inside is indicating using 1) or go outside (outside is indicating using 0).

So if there is only one door and it takes one time unit to use the door, there are few rules that we have to follow −

  • The door starts with 'in' position and then is set to the position used by the last participant.

  • If there's only one participant at the door at given time t, they can use the door.

  • If two or more participant want to go in, earliest participant goes first and then the direction previously used holds precedence.

  • If no one uses the door for one-time unit, it reverts back to the initial state.

So, we have to find the sorted list where each element contains [t, d], indicating at time t, a person either went inside or outside.

So, if the input is like [[2,0],[3,1],[6,0],[6,1],[3,0]], then the output will be [[2,0],[3,0],[4,1],[6,1],[7,0]]

To solve this, we will follow these steps −

  • sort the array v

  • create one list ret

  • curr := 1, i := 0, j := 0

  • n := size of v

  • while i < n, do:

    • if ret is not empty and v[i, 0] - t of last element of ret > 1, then:

      • curr := 1

    • j := i + 1

    • Define an array arr of size 2

    • (increase arr[v[i, 1]] by 1)

    • while (j < n and v[j, 0] is same as v[i, 0]), do −

      • (increase arr[v[j, 1]] by 1)

    • t := maximum of (if ret is empty, then 0, otherwise t of last element of ret) and v[i, 0]

    • if arr[1] is non-zero and arr[0] is non-zero, then −

      • while arr[curr] is non-zero, decrease arr[curr] by one in each step, do −

        • insert {t, curr} at the end of ret

        • (increase t by 1)

      • curr := curr XOR 1

      • while arr[curr] is non-zero, decrease arr[curr] by one in each step, do −

        • insert {t, curr} at the end of ret

        • (increase t by 1)

    • Otherwise

      • curr := v[i, 1]

      • while arr[curr] is non-zero, decrease arr[curr] by one in each step, do −

        • insert {t, curr} at the end of ret

        • (increase t by 1)

    • curr := direction of last element of ret

    • i := j

  • return ret

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<vector<auto>> v) {
   cout << "[";
   for (int i = 0; i < v.size(); i++) {
      cout << "[";
      for (int j = 0; j < v[i].size(); j++) {
         cout << v[i][j] << ", ";
      }
      cout << "],";
   }
   cout << "]" << endl;
}
class Solution {
   public:
   vector<vector<int>> solve(vector<vector<int>>& v) {
      sort(v.begin(), v.end());
      vector < vector <int > > ret;
      int curr = 1;
      int i = 0;
      int j = 0;
      int n = v.size();
      while(i < n){
         if(!ret.empty() && v[i][0] - ret.back()[0] > 1){
            curr = 1;
         }
         j = i + 1;
         vector <int> arr(2);
         arr[v[i][1]]++;
         while(j < n && v[j][0] == v[i][0]){
            arr[v[j][1]]++;
            j++;
         }
         int t = max((ret.empty()? 0 : ret.back()[0] + 1), v[i][0]);
         if(arr[1] && arr[0]){
            while(arr[curr]--){
               ret.push_back({t, curr});
               t++;
            }
            curr = curr ^ 1;
            while(arr[curr]--){
               ret.push_back({t, curr});
               t++;
            }
         }else{
            curr = v[i][1];
            while(arr[curr]--){
               ret.push_back({t, curr});
               t++;
            }
         }
         curr = ret.back()[1];
         i = j;
      }
      return ret;
   }
};
int main(){
   vector<vector<int>> v = {{2, 0},{3, 1},{6, 0},{6, 1},{3, 0}};
   Solution ob;
   print_vector(ob.solve(v));
}

Input

{{2, 0},{3, 1},{6, 0},{6, 1},{3, 0}}

Output

[[2, 0, ],[3, 0, ],[4, 1, ],[6, 1, ],[7, 0, ],]
raja
Updated on 02-Sep-2020 11:59:33

Advertisements