A person can climb to a nth floor in steps of 1, 2 or 3. Count number of ways a person can reach 12th floor.

743 Views Asked by At

Example:A person can reach 1st floor in only 1 way, 2nd floor in 2 ways, 3rd floor in 4 ways and 4th floor in 7 ways.
Using recursive calls i got answer as 927 but there is no such option.
1023
734
827
69

1

There are 1 best solutions below

0
On

I got 927 with the following SQL run on MS SQL server

select DS.* into #S from (select 1 x union select 2 union select 3 union select 0) DS

select DISTINCT REPLACE(cast(a1.x as varchar(1))  
        + cast(a2.x as varchar(1)) 
        + cast(a3.x as varchar(1)) 
        + cast(a4.x as varchar(1)) 
        + cast(a5.x as varchar(1)) 
        + cast(a6.x as varchar(1)) 
        + cast(a7.x as varchar(1)) 
        + cast(a8.x as varchar(1)) 
        + cast(a9.x as varchar(1)) 
        + cast(a10.x as varchar(1)) 
        + cast(a11.x as varchar(1)) 
        + cast(a12.x as varchar(1)) , '0', '') 
         from #s a1, #s a2, #s a3 , #s a4 , #s a5 , #s a6 , #s a7 , #s a8 , #s a9 , #s a10 , #s a11 , #s a12  
            where a1.x + a2.x + a3.x+ a4.x+ a5.x+ a6.x+ a7.x+ a8.x+ a9.x+ a10.x+ a11.x+ a12.x  = 12


drop table #s

that is to say, I arranged all combinations of 0,1,2, or 3 steps 12 times, and selected combinations that add up to 12 - then I stripped out all countings of zero steps, leaving me with every permutation that adds to 12, e.g 33312 - then I listed the distinct values (1 row per unique occurrence) - that gave me 927 on my screen grid-view

I'm not looking for any BA or anything, this was too big for a comment.