A row of buildings runs west to east along a beach, and the sea is past the eastern end. A building has a sea view when it is strictly taller than every building east of it. In array terms these values are the leaders: each one is greater than everything to its right.
Write two functions that take an array of numbers nums, listed from west to east:
leaders(nums) returns the leader values, in their original left-to-right order.
leaderIndexes(nums) returns the indexes of the leaders, in increasing order.
Rules:
The last value is always a leader, because nothing is to its right. An empty array has no leaders: return [].
The comparison is strict: a value equal to something on its right is not a leader.
Do not change the input array.
If nums is not an array of finite numbers, throw a TypeError.
The tests use arrays of 200,000 values. Comparing each value with everything after it is O(n²) and will not finish in time.