Time: O(n*log(n)), Space: O(n)