v1 time O(n^3log(n)) space O(log(n)) Accepted