Given an integer array nums, find all leaders in the array. An element is a leader if it is strictly greater than all elements to its right. The rightmost element is always a leader.
Return the leaders in the order they appear in the array.
Function Signature
def find_leaders(nums: list[int]) -> list[int]:
...
Rules
-
The comparison is strict: an element that equals any value to its right is not a leader.
Constraints
-
1 <= len(nums) <= 10^5
-
-10^4 <= nums[i] <= 10^4
Examples
Input: nums = [16,17,4,3,5,2]
Output: [17,5,2]
17 is greater than 4, 3, 5 and 2, 5 is greater than 2, and 2 is the rightmost element. 16, 4 and 3 each have a larger value somewhere to their right.
Input: nums = [1,2,3,4,0]
Output: [4,0]
4 is greater than the 0 after it, and 0 is the rightmost element. Each of 1, 2 and 3 has a larger value to its right.
Input: nums = [5,3,5,1]
Output: [5,1]
The 5 in the output is the one at index 2. The 5 at index 0 is not a leader because an equal 5 sits to its right, and 3 is smaller than that 5.