Pyramid II

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/iov3Rain/article/details/90243346

Title Description

Pyramid is a well-known game in which a person should stand on another's shoulders. In order to mount into the more stable Rohan, we should let the people above more lightly than down below. Now a circus to perform this program for visual effects, we also require the following people taller than the person above. Please write an algorithm to calculate the maximum number of people can be folded, note where all the actors are present at the same time.

Given a two-dimensional int array of actors, each element has two values, representing an actor's height and weight. At the same time the total number of actors given n, go back up to the number stack. Guarantee equal to the total number is less than 500.

Test sample:

[[1,2],[3,4],[5,6],[7,8]],4

Returns: 4

 

Rise up sequence modification. On the issue pyramid I, together with the new conditions.

The pyramid I different actors here are all simultaneously.

Will be converted into a two-dimensional one-dimensional. The multidimensional dimension reduction, reduce complexity and difficulty.

Sort first by weight, height and then seek increased sequence, since the sequence results in weight must be ordered from small to large.

In turn, is also available.

 

class Stack {
public:
    int getHeight(vector<vector<int> > actors, int n) {
        // write code here
        if(n <= 0)
            return 0;
        sort(actors.begin(), actors.end(), [](vector<int> &v1, vector<int> &v2) ->bool{
            return v1[0] < v2[0];});
        vector<int> dp(n, 1);
        int ans = 1;
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < i; ++j)
            {
                if(actors[i][1] > actors[j][1])
                    dp[i] = max(dp[i], dp[j] + 1);
            }
            ans = max(dp[i], ans);
        }
        return ans;
    }
};

 

Guess you like

Origin blog.csdn.net/iov3Rain/article/details/90243346