Data Structures & AlgorithmsVery hard
Why is the number of Dyck-word stack sequences with n pushes and n pops equal to a Catalan number?
- A. Valid push/pop sequences are balanced parenthesis words, and balanced words of n pairs are counted by the Catalan number.
- B. Each push can be followed by any number of pops, which gives a power of two.
- C. The sequences are simply permutations of n elements.
- D. Each sequence must alternate push and pop, so there is only one.
Correct answer: A. Valid push/pop sequences are balanced parenthesis words, and balanced words of n pairs are counted by the Catalan number.
Pushes correspond to opening brackets and pops to closing ones, with the constraint that the stack never goes negative. Those balanced words are counted by C(n) = (2n)!/((n+1)!·n!).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A deque-based minimum of each sliding window runs in linear time because:
- How many ways are there to make 15 using coins of values 1, 2, 5 and 10, where the order of coins does not matter (so 5+5+5 and 5+10 are…
- A binary string "1011001010" should become non-decreasing (all 0s before all 1s). In one operation you flip a single character. What is…
- What is the length of the longest NON-DECREASING subsequence (equal neighbours allowed) of [5, 1, 3, 3, 2, 6, 6, 4, 7]?
- How many subsets of [2, 3, 5, 4, 6] (chosen by position) have a sum exactly equal to half of the total sum?
- A staircase has 8 steps. Each move climbs 1 or 2 steps, but you may NOT take three single-step moves in a row. How many different…