v3 time O(n) space O(log(n)) Accepted

was not clear about the question requirements