Stock Span implementation in O(n) time