There are people in a line filming a TikTok. You are given an integer array heights of size n that represents the heights of the people in the line.
The camera is to the right of the people. A person can be seen in the recording if the person can see the camera without obstructions. Formally, a person can be seen if all the people to their right have a smaller height.
Return a list of indices (0-indexed) of people that will be seen by the camera, sorted in increasing order.
Example 1:
Input: heights = [4,2,3,1]
Output: [0,2,3]
Explanation: Person 1 (0-indexed) will not be seen because person 2 is taller.
Example 2:
Input: heights = [4,3,2,1]
Output: [0,1,2,3]
Explanation: All the people will be seen by the camera!
Example 3:
Input: heights = [1,3,2,4]
Output: [3]
Explanation: Only person 3 will be seen by the camera.
这题的核心是从右往左扫描数组,维护右侧已经出现的最高身高;只有当前人的身高严格大于右侧最高值时,这个人才会被摄像头看到。用单调栈也可以,但本题最直接高效的方法是逆序遍历并记录“前缀最大值”的反向版本,时间复杂度 O(n),额外空间可做到 O(1)(不计输出)。