unifystockx / code_compete

Coding Competition with TDD and TypeScript
MIT License
0 stars 2 forks source link

Assignment - Find the waiting time for each student to go through the turnstile gate #24

Open ramjayunify opened 3 years ago

ramjayunify commented 3 years ago

A university has exactly one turnstile. It can be used either as an exit or an entrance. Unfortunately, sometimes many people want to pass through the turnstile and their directions can be different. The ith person comes to the turnstile at time[i] and wants to either exit the university if direction[i] = 1 or enter the university if direction[i] = 0. People form 2 queues, one to exit and one to enter. They are ordered by the time when they came to the turnstile and, if the times are equal, by their indices.

If some person wants to enter the university and another person wants to leave the university at the same moment, there are three cases:

  1. If in the previous second the turnstile was not used (maybe it was before, but not at the previous second), then the person who wants to leave goes first.
  2. If in the previous second the turnstile was used as an exit, then the person who wants to leave goes first.
  3. If in the previous second the turnstile was used as an entrance, then the person who wants to enter goes first.

Passing through the turnstile takes 1 second.

For each person, find the time when they will pass through the turnstile.

Example:

Input: time: [0,0,1,5] direction: [0,1,1,0]

Output: [2,0,1,5]