Maths: find the permutation number using one stack

This is more of a mathematical problem; I think nothing is programming.

Suppose I have stack, and I want to find permutationsnumbers 1,2,3,...n. I can pushand pop. for example, if n = 2: push,pop,push,pop1.2 and push,push,pop,pop2.1

if n = 4, I can get 14from permutations 24using stack. Does anyone know any function F(n)that can lead to a permutationsstack count (just one)? e.g. F (1) = 1

F (2) = 2

F (4) = 14

+3
source share
2 answers

, . N push ( "X" ) N pop ( "Y" ), :

  • (.. Y.... XXYYY... )

, - :

function F(n_push, n_pop) {
  int total_count = 0;

  if (n_push > 0) total_count += F(n_push - 1, n_pop);
  if (n_pop > n_push) total_count += F(n_push, n_pop - 1);

  return total_count;
}
0

Source: https://habr.com/ru/post/1785787/


All Articles