By Long Luo

# Intuition

The Data Pair $\textit{people}[h, k]$, $h_i$ represents the i-th person of height $h_i$, $k_i$ represents the other people in front who have a height greater than or equal to $h_i$.

Pattern:

When face such kind of problem with Data Pair, sorting first will simplify the problem.

Usually, we will sort the first element in ascending order, while the second element in descending order, or sort the first element in descending order with the second element in ascending order.

# Greedy

We sort the $\textit{people}$ pairs first by $height$ in descending order, and $k$ in ascending order.

According to the descending order of $height$, for each person, the number of people before him is the number of people whose height is greater than or equal to him.

According to the ascending order of $k$, because the $k$ is increase later, which can reduce the number of insert operations.

Take the example:

After sorting:

Insert one by one:

The code is as follows:

## Analysis

• Time Complexity: $O(nlogn)$.
• Space Complexity: $O(logn)$.

All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗