Analysis:
We maintain two lists, a store list and a list for candidates, both of them are all limited to 2 elements, then we update the candidate list first, because we don’t know if it is the final anser.
Time Complexity:
- O(n)
Space Complexity:
- O(1)
Code
1 | class Solution(object): |