Witness of The Tall People
Question
There are n people lined up, and each have a height represented as an integer. A murder has happened right in front of them, and only people who are taller than everyone in front of them are able to see what has happened. How many witnesses are there?
Example
1 | Input: [3, 6, 3, 4, 1] |
Solution
We need to traversal backwards and maintain the max during the process.
Code
1 | // time: O(n) space: O(1) |