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

didn't fully understand the question in v1

be careful