Paste Search Dynamic
count_combination
  1. count = 0
  2. def count_combinations(n, opened, close)
  3.         if close == n
  4.                 puts 'I am in base case'
  5.                 count += 1
  6.                 return
  7.         end
  8.         puts opened, close
  9.         if opened < n
  10.                 count_combinations(n, opened+1, close)
  11.         end
  12.         if opened < close
  13.                 count_combinations(n, opened, close+1)
  14.         end
  15. end
  16.  
  17. # count_combinations(1,1)
  18. # count_combinations(2,1)
  19. # count_combinations(1,1)
  20. # count_combinations(2,1)
  21. # count_combinations(2,2)
  22.  
  23. n = 2
  24. opened = 0
  25. close = 0
  26. ans = count_combinations(n, opened, close)
  27. puts count
Parsed in 0.004 seconds