1 Replies - 646 Views - Last Post: 13 October 2019 - 12:38 PM Rate Topic: -----

#1 alecsander   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 19

How to solve this competition challange with ruby?

Posted 13 October 2019 - 12:32 PM

Problem Statement

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 direciton[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:

If in the previous second the turnstile was not used (maybe it was used before, but not at the previous second), then the person who wants to leave goes first.
If in the previous second the turnstile was used as an exit, then the person who
wants to leave goes first
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

The function must return an array of n integers where the value at index[i] is the same when the ith person will pass the turnstile

The function has the following params:

time: an array of n integers where the value at index i is the time when the
ith person will came to the turnstile
direction: an array of n integers where the value at index i is the direction
of the ith person

Constraints

1 <= n <= 105
0 <= time[i] <= 109 for 0 <= i <= n 1
time[i] <= time[i+1] for 0 <= i <= n - 2
0 <= direction [i] <= 1 for 0 <= i <= n 1

Example:

n = 4

time = [0,0,1,5]

direction = [0,1,1,0]

Output = [2,0,1,5]


Example 2

n = 5

time = [0,1,1,3,3]


direction = [0,1,0,0,1]


Output = [0,2,1,4,3]

------

This is my actual trying

def getTimes(time, direction)
  persons = time.size - 1
  exits = []
  (0..persons).each do |person|
    if time[person] == time[person + 1]
      exits[person + 1] = time[person] if direction[person + 1] == 1 and direction[person] == 0
    else
      exits[person] = time[person] if direction[person] != direction[person + 1]
    end
  end

  p exits
end



And my response [nil, 0, 1, 5]

Is This A Good Question/Topic? 0
  • +

Replies To: How to solve this competition challange with ruby?

#2 alecsander   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 19

Re: How to solve this competition challange with ruby?

Posted 13 October 2019 - 12:38 PM

View Postalecsander, on 13 October 2019 - 12:32 PM, said:

Correct constraints

Constraints

1 <= n <= 105
0 <= time[i] <= 109 for 0 <= i <= n 1
time[i] <= time[i+1] for 0 <= i <= n - 2
0 <= direction [i] <= 1 for 0 <= i <= n 1

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1