Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maths: Find permutation number using one stack

It's more a math problem I guess, nothing programming.

Suppose I have a stack and I want to find the permutations of numbers 1,2,3,...n. I can push and pop. e.g. if n=2: push,pop,push,pop 1,2 and push,push,pop,pop 2,1

if n=4 I can only get 14 from the 24 permutations using the stack.. Does anyone know any function F(n) that could produce the number of permutations the stack (only one) can produce? eg f(1)=1

f(2)=2

f(4)=14

like image 251
alhid Avatar asked Dec 16 '25 16:12

alhid


1 Answers

Such function is a Catalan number. See http://en.wikipedia.org/wiki/Catalan_number for the formula.

like image 64
konrad.kruczynski Avatar answered Dec 19 '25 06:12

konrad.kruczynski