A Python list is an array: its values sit side by side in memory, so nums[i] is instant and a for loop visits every element once. Most array problems are solved by one careful pass while remembering a little state.
Given a list of integers nums, return the second largest distinct value. If there is no such value (fewer than two distinct numbers), return None.
Examples
Input: nums = [3, 1, 4, 1, 5]
Output: 4
Explanation: the distinct values are 1, 3, 4, 5; the second largest is 4.
Input: nums = [2, 2, 2]
Output: None
Explanation: there is only one distinct value.
Input: nums = [1, 2]
Output: 1
Constraints
0 <= len(nums) <= 1000-10**4 <= nums[i] <= 10**4
Goals
- Walk a list once while keeping track of the best values seen so far
- Handle duplicates and lists that are too short to have an answer