Problem 237948 · medium · Phase 02 Linear Data Structures

Leaders

arrays · best-so-far · reverse-traversal

An element of nums is a leader if it is strictly greater than every element to its right. The last element is always a leader. Return all leaders in the order they appear in nums.

Examples

Input:  nums = [16, 17, 4, 3, 5, 2]
Output: [17, 5, 2]

Input:  nums = [1, 2, 3]
Output: [3]

Constraints

  • 0 <= len(nums) <= 10**5
  • Target: O(n) time.

Goals

  • Traverse from right to left with a running maximum
  • Produce results in the original order despite scanning backwards
Starting Python…