O(input_size), use stack to store ascending indexes