We iterate from a brute-force scan to a two-pass sweep that walks a running max from each end. The "two-pass running max" pattern handles any "greater than everything to one side" question in linear time.
We iterate from a brute-force scan to a two-pass sweep that walks a running max from each end. The "two-pass running max" pattern handles any "greater than everything to one side" question in linear time.
You are given an integer array nums.
An element nums[i] is considered valid if it satisfies at least one of the following conditions:
The first and last elements are always valid.
Return an array of all valid elements in the same order as they appear in nums.