Dynamic programming question

Viewed 3362

A circus is designing a tower routine consisting of people standing atop one another’s shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower.

EXAMPLE:
Input (ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110)
Output: The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)

Someone suggested me the following: It can be done as follows:

  1. Sort the input in decreasing order of weight and find the longest decreasing sequence of hight.
  2. Sort the input in decreasing order of hight and find the longest decreasing sequence of weight.

Take max of 1 and 2.

I dont understand why we need to do both steps 1 and 2. Cant we just do 1 and find the answer. IF not , please give example in which doing only step 1 does not give answer?

5 Answers
Related