Stacked Carts

There are carts arranged from 1 to N N on the left track. There are two commands available to the operator:

  • push : move the rightmost cart from the left track to the top of the spur track
  • pop : move the topmost cart from the spur track to the right track.

Using these commands, in how many ways can we permute the carts as they are being queued up in the right track? Assume there are 16 carts.

For example, let us say there are 3 carts. Consider the sequence of commands push, pop, push, push, pop, pop

  • push pushes cart 3 to the spur track.
  • pop pops cart 3 to the right track.
  • push pushes cart 2 and cart 1 to the spur track.
  • pop pops cart 1 and cart 2 to the right track.

The new sequence of carts on the right track is [2, 1, 3] .

It turns out, that there are 5 ways the new sequence of carts can be in, when N = 3 N = 3


The answer is 35357670.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Let us try a recursive solution first:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
n = 16
result = 0

def count(spur, right):
  if right == n:  #all trains are on the right track
    result++
  elif right + spur < n: #there is at least one train available on the left track
    count(spur+1, right) #push a cart
  if spur > 0: #if the spur is non-empty
    count(spur-1, right+1) #pop a cart

count(0,0)
print result

But that is probably an inefficient solution. Let's try something else.


Consider any valid sequence of push and pop . The following must be true:

  • There are as many push es as pop s. Otherwise, we'd get a stackoverflow or keep a cart in the spur forever.
  • No cart can be pop ed before it was push ed.

And now consider the well known properties of bracketed sequences like (())()() . They have the same rules:

  • There are as many ( as ) .
  • There is no ) that precedes its corresponding ( .

So, if we replaced pop s and push es with ( and ) , we'd get a bracketed sequence.

As we are aware of, the number of such sequences are the Catalan Numbers

Nice problem and solution.

Christopher Boo - 4 years, 10 months ago
Christopher Boo
Jul 21, 2016

As a Dynamic Programming lover, my solution is a little different from Agnishom's.

Let d p ( i ) dp(i) be the number of permutations we can have for i i carts. Clearly, d p ( 0 ) = d p ( 1 ) = 1 dp(0) = dp(1) = 1 . For i > 1 i>1 , we have the following recurrence relation,

d p ( i ) = j = 0 i 1 d p ( j ) d p ( i j 1 ) dp(i)=\sum_{j=0}^{i-1} dp(j) * dp(i-j-1)

Why does this work? When we added another element, consider the moment it goes to the right track. if it is on the j th j^{\text{th}} position (0-indexed) that means there are j j elements infront of it and they have d p ( j ) dp(j) number of permutations which we have already compute. Similarly, behind it will have i j 1 i-j-1 elements with d p ( i j 1 ) dp(i-j-1) number of permutations.

Same solution as mine.

Ronak Agarwal - 4 years, 10 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...