Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum number of train station stops

I received this interview question and got stuck on it:

There are an infinite number of train stops starting from station number 0.

There are an infinite number of trains. The nth train stops at all of the k * 2^(n - 1) stops where k is between 0 and infinity.

When n = 1, the first train stops at stops 0, 1, 2, 3, 4, 5, 6, etc.

When n = 2, the second train stops at stops 0, 2, 4, 6, 8, etc.

When n = 3, the third train stops at stops 0, 4, 8, 12, etc.

Given a start station number and end station number, return the minimum number of stops between them. You can use any of the trains to get from one stop to another stop.

For example, the minimum number of stops between start = 1 and end = 4 is 3 because we can get from 1 to 2 to 4.

I'm thinking about a dynamic programming solution that would store in dp[start][end] the minimum number of steps between start and end. We'd build up the array using start...mid1, mid1...mid2, mid2...mid3, ..., midn...end. But I wasn't able to get it to work. How do you solve this?

Clarifications:

  1. Trains can only move forward from a lower number stop to a higher number stop.
  2. A train can start at any station where it makes a stop at.
  3. Trains can be boarded in any order. The n = 1 train can be boarded before or after boarding the n = 3 train.
  4. Trains can be boarded multiple times. For example, it is permitted to board the n = 1 train, next board the n = 2 train, and finally board the n = 1 train again.
like image 902
user9292787 Avatar asked Jan 31 '18 06:01

user9292787


People also ask

What is the smallest train station in the world?

Beauly railway station - Wikipedia.

What is called a train that stops at every station?

Passenger Train – A train that stops at every station. Express Train – A train that stops at selected stations and is comparatively faster than passenger train. Supper Fast Train – A train, with a speed more than 100 km/NR.

How much time we can stay in railway station after train arrival?

According to rules, it is 3 hours before departure or after the arrival of your train. One should possess a valid ticket with a PNR number.


2 Answers

I don't think you need dynamic programming at all for this problem. It can basically be expressed by binary calculations.

If you convert the number of a station to binary it tells you right away how to get there from station 0, e.g.,

station 6 = 110

tells you that you need to take the n=3 train and the n=2 train each for one station. So the popcount of the binary representation tells you how many steps you need.

The next step is to figure out how to get from one station to another. I´ll show this again by example. Say you want to get from station 7 to station 23.

station 7 = 00111

station 23 = 10111

The first thing you want to do is to get to an intermediate stop. This stop is specified by

(highest bits that are equal in start and end station) + (first different bit) + (filled up with zeros)

In our example the intermediate stop is 16 (10000). The steps you need to make can be calculated by the difference of that number and the start station (7 = 00111). In our example this yields

10000 - 00111 = 1001

Now you know, that you need 2 stops (n=1 train and n=4) to get from 7 to 16. The remaining task is to get from 16 to 23, again this can be solved by the corresponding difference

10111 - 10000 = 00111

So, you need another 3 stops to go from 16 to 23 (n= 3, n= 2, n= 1). This gives you 5 stops in total, just using two binary differences and the popcount. The resulting path can be extracted from the bit representations 7 -> 8 -> 16 -> 20 -> 22 -> 23

Edit:

For further clarification of the intermediate stop let's assume we want to go from

station 5 = 101 to

station 7 = 111

the intermediate stop in this case will be 110, because

highest bits that are equal in start and end station = 1

first different bit = 1

filled up with zeros = 0

we need one step to go there (110 - 101 = 001) and one more to go from there to the end station (111 - 110 = 001).

About the intermediate stop

The concept of the intermediate stop is a bit clunky but I could not find a more elegant way in order to get the bit operations to work. The intermediate stop is the stop in between start and end where the highest level bit switches (that's why it is constructed the way it is). In this respect it is the stop at which the fastest train (between start and end) operates (actually all trains that you are able to catch stop there).

By subtracting the intermediate stop (bit representation) from the end station (bit representation) you reduce the problem to the simple case starting from station 0 (cf. first example of my answer).

By subtracting the start station from the intermediate stop you also reduce the problem to the simple case, but assume that you go from the intermediate stop to the start station which is equivalent to the other way round.

like image 123
SaiBot Avatar answered Sep 21 '22 15:09

SaiBot


First, ask if you can go backward. It sounds like you can't, but as presented here (which may not reflect the question as you received it), the problem never gives an explicit direction for any of these trains. (I see you've now edited your question to say you can't go backward.)

Assuming you can't go backward, the strategy is simple: always take the highest-numbered available train that doesn't overshoot your destination.

Suppose you're at stop s, and the highest-numbered train that stops at your current location and doesn't overshoot is train k. Traveling once on train k will take you to stop s + 2^(k-1). There is no faster way to get to that stop, and no way to skip that stop - no lower-numbered trains skip any of train k's stops, and no higher-numbered trains stop between train k's stops, so you can't get on a higher-numbered train before you get there. Thus, train k is your best immediate move.

With this strategy in mind, most of the remaining optimization is a matter of efficient bit twiddling tricks to compute the number of stops without explicitly figuring out every stop on the route.

like image 40
user2357112 supports Monica Avatar answered Sep 21 '22 15:09

user2357112 supports Monica